]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/Dialog.cpp
* fix spelling in comments to please John.
[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         lyx::dispatch(fr);
62 }
63
64
65 void Dialog::updateDialog() const
66 {
67         dispatch(FuncRequest(LFUN_DIALOG_UPDATE, fromqstr(name_)));
68 }
69
70
71 void Dialog::disconnect() const
72 {
73         lyxview_->disconnectDialog(fromqstr(name_));
74 }
75
76
77 bool Dialog::isBufferAvailable() const
78 {
79         return lyxview_->currentBufferView() != 0;
80 }
81
82
83 bool Dialog::isBufferReadonly() const
84 {
85         if (!lyxview_->documentBufferView())
86                 return true;
87         return lyxview_->documentBufferView()->buffer().isReadonly();
88 }
89
90
91 QString Dialog::bufferFilepath() const
92 {
93         return toqstr(buffer().filePath());
94 }
95
96
97 KernelDocType Dialog::docType() const
98 {
99         if (buffer().isLatex())
100                 return LATEX;
101         if (buffer().isLiterate())
102                 return LITERATE;
103
104         return DOCBOOK;
105 }
106
107
108 BufferView const * Dialog::bufferview() const
109 {
110         return lyxview_->currentBufferView();
111 }
112
113
114 Buffer const & Dialog::buffer() const
115 {
116         LASSERT(lyxview_->currentBufferView(), /**/);
117         return lyxview_->currentBufferView()->buffer();
118 }
119
120
121 void Dialog::showData(string const & data)
122 {
123         if (isBufferDependent() && !isBufferAvailable())
124                 return;
125
126         if (!initialiseParams(data)) {
127                 LYXERR0("Dialog \"" << name()
128                         << "\" failed to translate the data string passed to show()");
129                 return;
130         }
131
132         showView();
133 }
134
135
136 void Dialog::apply()
137 {
138         if (isBufferDependent()) {
139                 if (!isBufferAvailable() ||
140                     (isBufferReadonly() && !canApplyToReadOnly()))
141                         return;
142         }
143
144         applyView();
145         dispatchParams();
146
147         if (disconnectOnApply() && !isClosing()) {
148                 disconnect();
149                 initialiseParams(string());
150                 updateView();
151         }
152 }
153
154
155 void Dialog::prepareView()
156 {
157         // Make sure the dialog controls are correctly enabled/disabled with
158         // readonly status.
159         checkStatus();
160
161         QWidget * w = asQWidget();
162         w->setWindowTitle(title_);
163
164         QSize const hint = w->sizeHint();
165         if (hint.height() >= 0 && hint.width() >= 0)
166                 w->setMinimumSize(hint);
167 }
168
169
170 void Dialog::showView()
171 {
172         prepareView();
173
174         QWidget * w = asQWidget();
175         if (w->isVisible()) {
176                 w->raise();
177                 w->activateWindow();
178         } else
179                 w->show();
180
181         if (wantInitialFocus())
182                 w->setFocus();
183         else {
184                 lyxview_->raise();
185                 lyxview_->activateWindow();
186                 lyxview_->setFocus();
187         }
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 Inset const * Dialog::inset(InsetCode code) const
209 {
210         Inset * ins = bufferview()->cursor().innerInsetOfType(code);
211         if (!ins)
212                 ins = bufferview()->cursor().nextInset();
213         if (!ins || ins->lyxCode() != code)
214                 return 0;
215         return ins;
216 }
217
218
219 void Dialog::checkStatus()
220 {
221         // buffer independent dialogs are always active.
222         // This check allows us leave canApply unimplemented for some dialogs.
223         if (!isBufferDependent()) {
224                 updateView();
225                 return;
226         }
227
228         // deactivate the dialog if we have no buffer
229         if (!isBufferAvailable()) {
230                 enableView(false);
231                 return;
232         }
233
234         // check whether this dialog may be active
235         if (canApply()) {
236                 bool const readonly = isBufferReadonly();
237                 enableView(!readonly || canApplyToReadOnly());
238                 // refreshReadOnly() is too generous in _enabling_ widgets
239                 // update dialog to disable disabled widgets again
240
241                 updateView();
242         } else
243                 enableView(false);
244 }
245
246
247 QString Dialog::sessionKey() const
248 {
249         return "views/" + QString::number(lyxview_->id())
250                 + "/" + name();
251 }
252
253
254 void Dialog::saveSession() const
255 {
256         QSettings settings;
257         settings.setValue(sessionKey() + "/geometry", asQWidget()->saveGeometry());
258 }
259
260
261 void Dialog::restoreSession()
262 {
263         QSettings settings;
264         asQWidget()->restoreGeometry(
265                 settings.value(sessionKey() + "/geometry").toByteArray());
266 }
267
268 } // namespace frontend
269 } // namespace lyx