]> git.lyx.org Git - lyx.git/blob - src/frontends/qt/Dialog.cpp
Update sk.po
[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, Qt::FocusReason reason)
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(reason);
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(Qt::FocusReason reason)
173 {
174         prepareView();
175
176         QWidget * w = asQWidget();
177         if (!w->isVisible()) {
178                 w->setFocus(reason);
179                 w->show();
180         }
181         w->raise();
182         w->activateWindow();
183         if (wantInitialFocus())
184                 w->setFocus(reason);
185         else {
186                 lyxview_.raise();
187                 lyxview_.activateWindow();
188                 lyxview_.setFocus(reason);
189         }
190 }
191
192 void Dialog::hideView()
193 {
194         QWidget * w = asQWidget();
195         if (!w->isVisible())
196                 return;
197         clearParams();
198         disconnect();
199         w->hide();
200 }
201
202
203 bool Dialog::isVisibleView() const
204 {
205         return asQWidget()->isVisible();
206 }
207
208
209 Inset const * Dialog::inset(InsetCode code) const
210 {
211         // ins: the innermost inset of the type we look for
212         //      that contains the cursor
213         Inset * ins = bufferview()->cursor().innerInsetOfType(code);
214         // next: a potential inset at cursor position
215         Inset * next = bufferview()->cursor().nextInset();
216         // Check if next is of the type we look for
217         if (next)
218                 if (next->lyxCode() != code)
219                         next = nullptr;
220         if (ins) {
221                 // prefer next if it is of the requested type (bug 8716)
222                 if (next)
223                         ins = next;
224         } else
225                 // no containing inset of requested type
226                 // use next (which might also be 0)
227                 ins = next;
228         return ins;
229 }
230
231
232 void Dialog::checkStatus()
233 {
234         // buffer independent dialogs are always active.
235         // This check allows us leave canApply unimplemented for some dialogs.
236         if (!isBufferDependent()) {
237                 updateView();
238                 return;
239         }
240
241         // deactivate the dialog if we have no buffer
242         if (!isBufferAvailable()) {
243                 enableView(false);
244                 return;
245         }
246
247         // check whether this dialog may be active
248         if (canApply()) {
249                 bool const readonly = isBufferReadonly();
250                 enableView(!readonly || canApplyToReadOnly());
251                 updateView();
252         } else
253                 enableView(false);
254 }
255
256
257 QString Dialog::sessionKey() const
258 {
259         return "views/" + QString::number(lyxview_.id())
260                 + "/" + name();
261 }
262
263
264 void Dialog::saveSession(QSettings & settings) const
265 {
266         settings.setValue(sessionKey() + "/geometry", asQWidget()->saveGeometry());
267 }
268
269
270 void Dialog::restoreSession()
271 {
272         QSettings settings;
273         asQWidget()->restoreGeometry(
274                 settings.value(sessionKey() + "/geometry").toByteArray());
275 }
276
277
278 // If we have just created an inset, then we want to attach the
279 // dialog to it. This (i) allows further modification of that inset and
280 // (ii) prevents an additional click on Apply or OK from unexpectedly
281 // creating another inset. (See #3964 and #11030.)
282 void Dialog::connectToNewInset()
283 {
284         GuiView & view = const_cast<GuiView &>(lyxview());
285         BufferView * bv = view.currentBufferView();
286         // should have one, but just to be safe...
287         if (!bv)
288                 return;
289
290         // are we attached to an inset already?
291         Inset * ins = bv->editedInset(fromqstr(name_));
292         if (ins)
293                 return;
294
295         // no, so we just inserted one, and now we are behind it.
296         Cursor const & cur = bv->cursor();
297         ins = cur.prevInset();
298         if (ins)
299                 bv->editInset(fromqstr(name_), ins);
300 }
301
302 } // namespace frontend
303 } // namespace lyx