]> git.lyx.org Git - lyx.git/blob - src/frontends/qt/Dialog.cpp
Fix readability
[lyx.git] / src / frontends / qt / Dialog.cpp
1 /**
2  * \file Dialog.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "Dialog.h"
14
15 #include "GuiView.h"
16 #include "qt_helpers.h"
17
18 #include "Buffer.h"
19 #include "BufferParams.h"
20 #include "BufferView.h"
21 #include "Cursor.h"
22 #include "FuncRequest.h"
23 #include "FuncStatus.h"
24 #include "LyX.h"
25
26 #include "support/debug.h"
27 #include "support/gettext.h"
28 #include "support/lassert.h"
29
30 #include <QSettings>
31
32 #include <string>
33
34 using namespace std;
35 using namespace lyx::support;
36
37 namespace lyx {
38 namespace frontend {
39
40 Dialog::Dialog(GuiView & lv, QString const & name, QString const & title)
41         : name_(name), title_(title), lyxview_(lv)
42 {}
43
44
45 bool Dialog::canApply() const
46 {
47         FuncRequest const fr(getLfun(), fromqstr(name_));
48         FuncStatus const fs(getStatus(fr));
49         return fs.enabled();
50 }
51
52
53 void Dialog::dispatch(FuncRequest const & fr) const
54 {
55         lyx::dispatch(fr);
56 }
57
58
59 void Dialog::updateDialog() const
60 {
61         dispatch(FuncRequest(LFUN_DIALOG_UPDATE, fromqstr(name_)));
62 }
63
64
65 void Dialog::disconnect() const
66 {
67         lyxview_.disconnectDialog(fromqstr(name_));
68 }
69
70
71 bool Dialog::isBufferAvailable() const
72 {
73         return lyxview_.currentBufferView() != nullptr;
74 }
75
76
77 bool Dialog::isBufferReadonly() const
78 {
79         if (!lyxview_.documentBufferView())
80                 return true;
81         return lyxview_.documentBufferView()->buffer().isReadonly();
82 }
83
84
85 QString Dialog::bufferFilePath() const
86 {
87         return toqstr(buffer().filePath());
88 }
89
90
91 KernelDocType Dialog::docType() const
92 {
93         if (buffer().params().isLatex())
94                 return KernelDocType::LaTeX;
95         if (buffer().params().isLiterate())
96                 return KernelDocType::Literate;
97
98         // This case should not happen.
99         return KernelDocType::LaTeX;
100 }
101
102
103 BufferView const * Dialog::bufferview() const
104 {
105         return lyxview_.currentBufferView();
106 }
107
108
109 Buffer const & Dialog::buffer() const
110 {
111         LAPPERR(lyxview_.currentBufferView());
112         return lyxview_.currentBufferView()->buffer();
113 }
114
115
116 Buffer const & Dialog::documentBuffer() const
117 {
118         LAPPERR(lyxview_.documentBufferView());
119         return lyxview_.documentBufferView()->buffer();
120 }
121
122
123 void Dialog::showData(string const & data)
124 {
125         if (isBufferDependent() && !isBufferAvailable())
126                 return;
127
128         if (!initialiseParams(data)) {
129                 LYXERR0("Dialog \"" << name()
130                         << "\" failed to translate the data string passed to show()");
131                 return;
132         }
133
134         showView();
135 }
136
137
138 void Dialog::apply()
139 {
140         if (isBufferDependent()) {
141                 if (!isBufferAvailable() ||
142                     (isBufferReadonly() && !canApplyToReadOnly()))
143                         return;
144         }
145
146         applyView();
147         dispatchParams();
148
149         if (disconnectOnApply() && !isClosing()) {
150                 disconnect();
151                 initialiseParams(string());
152                 updateView();
153         }
154 }
155
156
157 void Dialog::prepareView()
158 {
159         // Make sure the dialog controls are correctly enabled/disabled with
160         // readonly status.
161         checkStatus();
162
163         QWidget * w = asQWidget();
164         w->setWindowTitle(title_);
165
166         QSize const hint = w->sizeHint();
167         if (hint.height() >= 0 && hint.width() >= 0)
168                 w->setMinimumSize(hint);
169 }
170
171
172 void Dialog::showView()
173 {
174         prepareView();
175
176         QWidget * w = asQWidget();
177         if (!w->isVisible())
178                 w->show();
179         w->raise();
180         w->activateWindow();
181         if (wantInitialFocus())
182                 w->setFocus();
183         else {
184                 lyxview_.raise();
185                 lyxview_.activateWindow();
186                 lyxview_.setFocus();
187         }
188 }
189
190
191 void Dialog::hideView()
192 {
193         QWidget * w = asQWidget();
194         if (!w->isVisible())
195                 return;
196         clearParams();
197         disconnect();
198         w->hide();
199 }
200
201
202 bool Dialog::isVisibleView() const
203 {
204         return asQWidget()->isVisible();
205 }
206
207
208 Inset const * Dialog::inset(InsetCode code) const
209 {
210         // ins: the innermost inset of the type we look for
211         //      that contains the cursor
212         Inset * ins = bufferview()->cursor().innerInsetOfType(code);
213         // next: a potential inset at cursor position
214         Inset * next = bufferview()->cursor().nextInset();
215         // Check if next is of the type we look for
216         if (next)
217                 if (next->lyxCode() != code)
218                         next = nullptr;
219         if (ins) {
220                 // prefer next if it is of the requested type (bug 8716)
221                 if (next)
222                         ins = next;
223         } else
224                 // no containing inset of requested type
225                 // use next (which might also be 0)
226                 ins = next;
227         return ins;
228 }
229
230
231 void Dialog::checkStatus()
232 {
233         // buffer independent dialogs are always active.
234         // This check allows us leave canApply unimplemented for some dialogs.
235         if (!isBufferDependent()) {
236                 updateView();
237                 return;
238         }
239
240         // deactivate the dialog if we have no buffer
241         if (!isBufferAvailable()) {
242                 enableView(false);
243                 return;
244         }
245
246         // check whether this dialog may be active
247         if (canApply()) {
248                 bool const readonly = isBufferReadonly();
249                 enableView(!readonly || canApplyToReadOnly());
250                 updateView();
251         } else
252                 enableView(false);
253 }
254
255
256 QString Dialog::sessionKey() const
257 {
258         return "views/" + QString::number(lyxview_.id())
259                 + "/" + name();
260 }
261
262
263 void Dialog::saveSession(QSettings & settings) const
264 {
265         settings.setValue(sessionKey() + "/geometry", asQWidget()->saveGeometry());
266 }
267
268
269 void Dialog::restoreSession()
270 {
271         QSettings settings;
272         asQWidget()->restoreGeometry(
273                 settings.value(sessionKey() + "/geometry").toByteArray());
274 }
275
276
277 // If we have just created an inset, then we want to attach the
278 // dialog to it. This (i) allows further modification of that inset and
279 // (ii) prevents an additional click on Apply or OK from unexpectedly
280 // creating another inset. (See #3964 and #11030.)
281 void Dialog::connectToNewInset()
282 {
283         GuiView & view = const_cast<GuiView &>(lyxview());
284         BufferView * bv = view.currentBufferView();
285         // should have one, but just to be safe...
286         if (!bv)
287                 return;
288
289         // are we attached to an inset already?
290         Inset * ins = bv->editedInset(fromqstr(name_));
291         if (ins)
292                 return;
293
294         // no, so we just inserted one, and now we are behind it.
295         Cursor const & cur = bv->cursor();
296         ins = cur.prevInset();
297         if (ins)
298                 bv->editInset(fromqstr(name_), ins);
299 }
300
301 } // namespace frontend
302 } // namespace lyx