]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/Dialog.cpp
Speed up exit time
[lyx.git] / src / frontends / qt4 / 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() != 0;
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         return DOCBOOK;
110 }
111
112
113 BufferView const * Dialog::bufferview() const
114 {
115         return lyxview_->currentBufferView();
116 }
117
118
119 Buffer const & Dialog::buffer() const
120 {
121         LAPPERR(lyxview_->currentBufferView());
122         return lyxview_->currentBufferView()->buffer();
123 }
124
125
126 Buffer const & Dialog::documentBuffer() const
127 {
128         LAPPERR(lyxview_->documentBufferView());
129         return lyxview_->documentBufferView()->buffer();
130 }
131
132
133 void Dialog::showData(string const & data)
134 {
135         if (isBufferDependent() && !isBufferAvailable())
136                 return;
137
138         if (!initialiseParams(data)) {
139                 LYXERR0("Dialog \"" << name()
140                         << "\" failed to translate the data string passed to show()");
141                 return;
142         }
143
144         showView();
145 }
146
147
148 void Dialog::apply()
149 {
150         if (isBufferDependent()) {
151                 if (!isBufferAvailable() ||
152                     (isBufferReadonly() && !canApplyToReadOnly()))
153                         return;
154         }
155
156         applyView();
157         dispatchParams();
158
159         if (disconnectOnApply() && !isClosing()) {
160                 disconnect();
161                 initialiseParams(string());
162                 updateView();
163         }
164 }
165
166
167 void Dialog::prepareView()
168 {
169         // Make sure the dialog controls are correctly enabled/disabled with
170         // readonly status.
171         checkStatus();
172
173         QWidget * w = asQWidget();
174         w->setWindowTitle(title_);
175
176         QSize const hint = w->sizeHint();
177         if (hint.height() >= 0 && hint.width() >= 0)
178                 w->setMinimumSize(hint);
179 }
180
181
182 void Dialog::showView()
183 {
184         prepareView();
185
186         QWidget * w = asQWidget();
187         if (w->isVisible()) {
188                 w->raise();
189                 w->activateWindow();
190         } else
191                 w->show();
192
193         if (wantInitialFocus())
194                 w->setFocus();
195         else {
196                 lyxview_->raise();
197                 lyxview_->activateWindow();
198                 lyxview_->setFocus();
199         }
200 }
201
202
203 void Dialog::hideView()
204 {
205         QWidget * w = asQWidget();
206         if (!w->isVisible())
207                 return;
208         clearParams();
209         disconnect();
210         w->hide();
211 }
212
213
214 bool Dialog::isVisibleView() const
215 {
216         return asQWidget()->isVisible();
217 }
218
219
220 Inset const * Dialog::inset(InsetCode code) const
221 {
222         // ins: the innermost inset of the type we look for
223         //      that contains the cursor
224         Inset * ins = bufferview()->cursor().innerInsetOfType(code);
225         // next: a potential inset at cursor position
226         Inset * next = bufferview()->cursor().nextInset();
227         // Check if next is of the type we look for
228         if (next)
229                 if (next->lyxCode() != code)
230                         next = 0;
231         if (ins) {
232                 // prefer next if it is of the requested type (bug 8716)
233                 if (next)
234                         ins = next;
235         } else
236                 // no containing inset of requested type
237                 // use next (which might also be 0)
238                 ins = next;
239         return ins;
240 }
241
242
243 void Dialog::checkStatus()
244 {
245         // buffer independent dialogs are always active.
246         // This check allows us leave canApply unimplemented for some dialogs.
247         if (!isBufferDependent()) {
248                 updateView();
249                 return;
250         }
251
252         // deactivate the dialog if we have no buffer
253         if (!isBufferAvailable()) {
254                 enableView(false);
255                 return;
256         }
257
258         // check whether this dialog may be active
259         if (canApply()) {
260                 bool const readonly = isBufferReadonly();
261                 enableView(!readonly || canApplyToReadOnly());
262                 // refreshReadOnly() is too generous in _enabling_ widgets
263                 // update dialog to disable disabled widgets again
264
265                 updateView();
266         } else
267                 enableView(false);
268 }
269
270
271 QString Dialog::sessionKey() const
272 {
273         return "views/" + QString::number(lyxview_->id())
274                 + "/" + name();
275 }
276
277
278 void Dialog::saveSession(QSettings & settings) const
279 {
280         settings.setValue(sessionKey() + "/geometry", asQWidget()->saveGeometry());
281 }
282
283
284 void Dialog::restoreSession()
285 {
286         QSettings settings;
287         asQWidget()->restoreGeometry(
288                 settings.value(sessionKey() + "/geometry").toByteArray());
289 }
290
291 } // namespace frontend
292 } // namespace lyx