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