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