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