]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/Dialog.cpp
Complete the removal of the embedding stuff. Maybe. It's hard to be sure we got every...
[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 "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 "support/assert.h"
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, QString const & name, QString const & title)
40         : name_(name), title_(title), lyxview_(&lv)
41 {}
42
43
44 Dialog::~Dialog()
45 {}
46
47
48 bool Dialog::canApply() const
49 {
50         FuncRequest const fr(getLfun(), fromqstr(name_));
51         FuncStatus const fs(getStatus(fr));
52         return fs.enabled();
53 }
54
55
56 void Dialog::dispatch(FuncRequest const & fr) const
57 {
58         theLyXFunc().setLyXView(lyxview_);
59         lyx::dispatch(fr);
60 }
61
62
63 void Dialog::updateDialog() const
64 {
65         dispatch(FuncRequest(LFUN_DIALOG_UPDATE, fromqstr(name_)));
66 }
67
68
69 void Dialog::disconnect() const
70 {
71         lyxview_->disconnectDialog(fromqstr(name_));
72 }
73
74
75 bool Dialog::isBufferAvailable() const
76 {
77         return lyxview_->buffer() != 0;
78 }
79
80
81 bool Dialog::isBufferReadonly() const
82 {
83         if (!lyxview_->buffer())
84                 return true;
85         return lyxview_->buffer()->isReadonly();
86 }
87
88
89 QString Dialog::bufferFilepath() const
90 {
91         return toqstr(buffer().filePath());
92 }
93
94
95 KernelDocType Dialog::docType() const
96 {
97         if (buffer().isLatex())
98                 return LATEX;
99         if (buffer().isLiterate())
100                 return LITERATE;
101
102         return DOCBOOK;
103 }
104
105
106 BufferView * Dialog::bufferview()
107 {
108         return lyxview_->view();
109 }
110
111
112 BufferView const * Dialog::bufferview() const
113 {
114         return lyxview_->view();
115 }
116
117
118 Buffer & Dialog::buffer()
119 {
120         LASSERT(lyxview_->buffer(), /**/);
121         return *lyxview_->buffer();
122 }
123
124
125 Buffer const & Dialog::buffer() const
126 {
127         LASSERT(lyxview_->buffer(), /**/);
128         return *lyxview_->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 \"" << fromqstr(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::updateData(string const & data)
167 {
168         if (isBufferDependent() && !isBufferAvailable())
169                 return;
170
171         if (!initialiseParams(data)) {
172                 LYXERR0("Dialog \"" << fromqstr(name())
173                        << "\" could not be initialized");
174                 return;
175         }
176
177         updateView();
178 }
179
180
181 void Dialog::showView()
182 {
183         // Make sure the dialog is up-to-date.
184         updateView(); 
185         // Make sure the dialog controls are correctly enabled/disabled with
186         // readonly status.
187         checkStatus();
188         if (exitEarly())
189                 return;
190
191         QWidget * w = asQWidget();
192         w->setWindowTitle(title_);
193
194         QSize const hint = w->sizeHint();
195         if (hint.height() >= 0 && hint.width() >= 0)
196                 w->setMinimumSize(hint);
197
198         if (w->isVisible()) {
199                 w->raise();
200                 w->activateWindow();
201         } else
202                 w->show();
203
204         w->setFocus();
205 }
206
207
208 void Dialog::hideView()
209 {
210         QWidget * w = asQWidget();
211         if (!w->isVisible())
212                 return;
213         clearParams();
214         disconnect();
215         w->hide();
216 }
217
218
219 bool Dialog::isVisibleView() const
220 {
221         return asQWidget()->isVisible();
222 }
223
224
225 void Dialog::checkStatus()
226 {
227         // buffer independant dialogs are always active.
228         // This check allows us leave canApply unimplemented for some dialogs.
229         if (!isBufferDependent())
230                 return;
231
232         // deactivate the dialog if we have no buffer
233         if (!isBufferAvailable()) {
234                 enableView(false);
235                 return;
236         }
237
238         // check whether this dialog may be active
239         if (canApply()) {
240                 bool const readonly = isBufferReadonly();
241                 enableView(!readonly);
242                 // refreshReadOnly() is too generous in _enabling_ widgets
243                 // update dialog to disable disabled widgets again
244
245                 if (!readonly || canApplyToReadOnly())
246                         updateView();
247
248         } else
249                 enableView(false);
250 }
251
252
253 QString Dialog::sessionKey() const
254 {
255         return "view-" + QString::number(lyxview_->id())
256                 + "/" + name();
257 }
258
259
260 void Dialog::saveSession() const
261 {
262         QSettings settings;
263         settings.setValue(sessionKey() + "/geometry", asQWidget()->saveGeometry());
264 }
265
266
267 void Dialog::restoreSession()
268 {
269         QSettings settings;
270         asQWidget()->restoreGeometry(
271                 settings.value(sessionKey() + "/geometry").toByteArray());
272 }
273
274 } // namespace frontend
275 } // namespace lyx