]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/Dialog.cpp
fc741985d8c73a1fcffabfc94150d0e23e9931ab
[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/lassert.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 (!initialiseParams(data)) {
169                 LYXERR0("Dialog \"" << fromqstr(name())
170                        << "\" could not be initialized");
171                 return;
172         }
173
174         if (lyxview_->buffer())
175                 updateView();
176         else
177                 enableView(false);
178 }
179
180
181 void Dialog::showView()
182 {
183         // Make sure the dialog controls are correctly enabled/disabled with
184         // readonly status.
185         checkStatus();
186         if (exitEarly())
187                 return;
188
189         QWidget * w = asQWidget();
190         w->setWindowTitle(title_);
191
192         QSize const hint = w->sizeHint();
193         if (hint.height() >= 0 && hint.width() >= 0)
194                 w->setMinimumSize(hint);
195
196         if (w->isVisible()) {
197                 w->raise();
198                 w->activateWindow();
199         } else
200                 w->show();
201
202         w->setFocus();
203 }
204
205
206 void Dialog::hideView()
207 {
208         QWidget * w = asQWidget();
209         if (!w->isVisible())
210                 return;
211         clearParams();
212         disconnect();
213         w->hide();
214 }
215
216
217 bool Dialog::isVisibleView() const
218 {
219         return asQWidget()->isVisible();
220 }
221
222
223 void Dialog::checkStatus()
224 {
225         // buffer independant dialogs are always active.
226         // This check allows us leave canApply unimplemented for some dialogs.
227         if (!isBufferDependent())
228                 return;
229
230         // deactivate the dialog if we have no buffer
231         if (!isBufferAvailable()) {
232                 enableView(false);
233                 return;
234         }
235
236         // check whether this dialog may be active
237         if (canApply()) {
238                 bool const readonly = isBufferReadonly();
239                 enableView(!readonly);
240                 // refreshReadOnly() is too generous in _enabling_ widgets
241                 // update dialog to disable disabled widgets again
242
243                 if (!readonly || canApplyToReadOnly())
244                         updateView();
245
246         } else
247                 enableView(false);
248 }
249
250
251 QString Dialog::sessionKey() const
252 {
253         return "view-" + QString::number(lyxview_->id())
254                 + "/" + name();
255 }
256
257
258 void Dialog::saveSession() const
259 {
260         QSettings settings;
261         settings.setValue(sessionKey() + "/geometry", asQWidget()->saveGeometry());
262 }
263
264
265 void Dialog::restoreSession()
266 {
267         QSettings settings;
268         asQWidget()->restoreGeometry(
269                 settings.value(sessionKey() + "/geometry").toByteArray());
270 }
271
272 } // namespace frontend
273 } // namespace lyx