]> git.lyx.org Git - features.git/blob - src/frontends/qt/Dialog.cpp
#10571 improved handling of WM's signal when switching from or to full-screen window
[features.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 "insets/Inset.h"
27
28 #include "support/debug.h"
29 #include "support/gettext.h"
30 #include "support/lassert.h"
31
32 #include <QLabel>
33 #include <QLineEdit>
34 #include <QList>
35 #include <QSettings>
36 #include <QString>
37 #include <QValidator>
38
39 #include <string>
40
41 using namespace std;
42 using namespace lyx::support;
43
44 namespace lyx {
45 namespace frontend {
46
47 Dialog::Dialog(GuiView & lv, QString const & name, QString const & title)
48         : name_(name), title_(title), lyxview_(lv)
49 {}
50
51
52 Dialog::~Dialog()
53 {}
54
55
56 bool Dialog::canApply() const
57 {
58         FuncRequest const fr(getLfun(), fromqstr(name_));
59         FuncStatus const fs(getStatus(fr));
60         return fs.enabled();
61 }
62
63
64 void Dialog::dispatch(FuncRequest const & fr) const
65 {
66         lyx::dispatch(fr);
67 }
68
69
70 void Dialog::updateDialog() const
71 {
72         dispatch(FuncRequest(LFUN_DIALOG_UPDATE, fromqstr(name_)));
73 }
74
75
76 void Dialog::disconnect() const
77 {
78         lyxview_.disconnectDialog(fromqstr(name_));
79 }
80
81
82 bool Dialog::isBufferAvailable() const
83 {
84         return lyxview_.currentBufferView() != nullptr;
85 }
86
87
88 bool Dialog::isBufferReadonly() const
89 {
90         if (!lyxview_.documentBufferView())
91                 return true;
92         return lyxview_.documentBufferView()->buffer().isReadonly();
93 }
94
95
96 QString Dialog::bufferFilePath() const
97 {
98         return toqstr(buffer().filePath());
99 }
100
101
102 KernelDocType Dialog::docType() const
103 {
104         if (buffer().params().isLatex())
105                 return LATEX;
106         if (buffer().params().isLiterate())
107                 return LITERATE;
108
109         // This case should not happen.
110         return LATEX;
111 }
112
113
114 BufferView const * Dialog::bufferview() const
115 {
116         return lyxview_.currentBufferView();
117 }
118
119
120 Buffer const & Dialog::buffer() const
121 {
122         LAPPERR(lyxview_.currentBufferView());
123         return lyxview_.currentBufferView()->buffer();
124 }
125
126
127 Buffer const & Dialog::documentBuffer() const
128 {
129         LAPPERR(lyxview_.documentBufferView());
130         return lyxview_.documentBufferView()->buffer();
131 }
132
133
134 void Dialog::showData(string const & data)
135 {
136         if (isBufferDependent() && !isBufferAvailable())
137                 return;
138
139         if (!initialiseParams(data)) {
140                 LYXERR0("Dialog \"" << name()
141                         << "\" failed to translate the data string passed to show()");
142                 return;
143         }
144
145         showView();
146 }
147
148
149 void Dialog::apply()
150 {
151         if (isBufferDependent()) {
152                 if (!isBufferAvailable() ||
153                     (isBufferReadonly() && !canApplyToReadOnly()))
154                         return;
155         }
156
157         applyView();
158         dispatchParams();
159
160         if (disconnectOnApply() && !isClosing()) {
161                 disconnect();
162                 initialiseParams(string());
163                 updateView();
164         }
165 }
166
167
168 void Dialog::prepareView()
169 {
170         // Make sure the dialog controls are correctly enabled/disabled with
171         // readonly status.
172         checkStatus();
173
174         QWidget * w = asQWidget();
175         w->setWindowTitle(title_);
176
177         QSize const hint = w->sizeHint();
178         if (hint.height() >= 0 && hint.width() >= 0)
179                 w->setMinimumSize(hint);
180 }
181
182
183 void Dialog::showView()
184 {
185         prepareView();
186
187         QWidget * w = asQWidget();
188         if (w->isVisible()) {
189                 w->raise();
190                 w->activateWindow();
191         } else
192                 w->show();
193
194         if (wantInitialFocus())
195                 w->setFocus();
196         else {
197                 lyxview_.raise();
198                 lyxview_.activateWindow();
199                 lyxview_.setFocus();
200         }
201 }
202
203
204 void Dialog::hideView()
205 {
206         QWidget * w = asQWidget();
207         if (!w->isVisible())
208                 return;
209         clearParams();
210         disconnect();
211         w->hide();
212 }
213
214
215 bool Dialog::isVisibleView() const
216 {
217         return asQWidget()->isVisible();
218 }
219
220
221 Inset const * Dialog::inset(InsetCode code) const
222 {
223         // ins: the innermost inset of the type we look for
224         //      that contains the cursor
225         Inset * ins = bufferview()->cursor().innerInsetOfType(code);
226         // next: a potential inset at cursor position
227         Inset * next = bufferview()->cursor().nextInset();
228         // Check if next is of the type we look for
229         if (next)
230                 if (next->lyxCode() != code)
231                         next = nullptr;
232         if (ins) {
233                 // prefer next if it is of the requested type (bug 8716)
234                 if (next)
235                         ins = next;
236         } else
237                 // no containing inset of requested type
238                 // use next (which might also be 0)
239                 ins = next;
240         return ins;
241 }
242
243
244 void Dialog::checkStatus()
245 {
246         // buffer independent dialogs are always active.
247         // This check allows us leave canApply unimplemented for some dialogs.
248         if (!isBufferDependent()) {
249                 updateView();
250                 return;
251         }
252
253         // deactivate the dialog if we have no buffer
254         if (!isBufferAvailable()) {
255                 enableView(false);
256                 return;
257         }
258
259         // check whether this dialog may be active
260         if (canApply()) {
261                 bool const readonly = isBufferReadonly();
262                 enableView(!readonly || canApplyToReadOnly());
263                 updateView();
264         } else
265                 enableView(false);
266 }
267
268
269 QString Dialog::sessionKey() const
270 {
271         return "views/" + QString::number(lyxview_.id())
272                 + "/" + name();
273 }
274
275
276 void Dialog::saveSession(QSettings & settings) const
277 {
278         settings.setValue(sessionKey() + "/geometry", asQWidget()->saveGeometry());
279 }
280
281
282 void Dialog::restoreSession()
283 {
284         QSettings settings;
285         asQWidget()->restoreGeometry(
286                 settings.value(sessionKey() + "/geometry").toByteArray());
287 }
288
289 } // namespace frontend
290 } // namespace lyx