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