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