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