]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/Dialog.cpp
Move the buffer related part from GuiView::renameBuffer to Buffer::saveAs.
[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 "LyX.h"
24
25 #include "insets/Inset.h"
26
27 #include "support/debug.h"
28 #include "support/lassert.h"
29
30 #include <QLabel>
31 #include <QLineEdit>
32 #include <QList>
33 #include <QSettings>
34 #include <QString>
35 #include <QValidator>
36
37 #include <string>
38
39 using namespace std;
40 using namespace lyx::support;
41
42 namespace lyx {
43 namespace frontend {
44
45 Dialog::Dialog(GuiView & lv, QString const & name, QString const & title)
46         : name_(name), title_(title), lyxview_(&lv)
47 {}
48
49
50 Dialog::~Dialog()
51 {}
52
53
54 bool Dialog::canApply() const
55 {
56         FuncRequest const fr(getLfun(), fromqstr(name_));
57         FuncStatus const fs(getStatus(fr));
58         return fs.enabled();
59 }
60
61
62 void Dialog::dispatch(FuncRequest const & fr) const
63 {
64         lyx::dispatch(fr);
65 }
66
67
68 void Dialog::updateDialog() const
69 {
70         dispatch(FuncRequest(LFUN_DIALOG_UPDATE, fromqstr(name_)));
71 }
72
73
74 void Dialog::disconnect() const
75 {
76         lyxview_->disconnectDialog(fromqstr(name_));
77 }
78
79
80 bool Dialog::isBufferAvailable() const
81 {
82         return lyxview_->currentBufferView() != 0;
83 }
84
85
86 bool Dialog::isBufferReadonly() const
87 {
88         if (!lyxview_->documentBufferView())
89                 return true;
90         return lyxview_->documentBufferView()->buffer().isReadonly();
91 }
92
93
94 QString Dialog::bufferFilePath() const
95 {
96         return toqstr(buffer().filePath());
97 }
98
99
100 KernelDocType Dialog::docType() const
101 {
102         if (buffer().isLatex())
103                 return LATEX;
104         if (buffer().isLiterate())
105                 return LITERATE;
106
107         return DOCBOOK;
108 }
109
110
111 BufferView const * Dialog::bufferview() const
112 {
113         return lyxview_->currentBufferView();
114 }
115
116
117 Buffer const & Dialog::buffer() const
118 {
119         LASSERT(lyxview_->currentBufferView(), /**/);
120         return lyxview_->currentBufferView()->buffer();
121 }
122
123
124 void Dialog::showData(string const & data)
125 {
126         if (isBufferDependent() && !isBufferAvailable())
127                 return;
128
129         if (!initialiseParams(data)) {
130                 LYXERR0("Dialog \"" << name()
131                         << "\" failed to translate the data string passed to show()");
132                 return;
133         }
134
135         showView();
136 }
137
138
139 void Dialog::apply()
140 {
141         if (isBufferDependent()) {
142                 if (!isBufferAvailable() ||
143                     (isBufferReadonly() && !canApplyToReadOnly()))
144                         return;
145         }
146
147         applyView();
148         dispatchParams();
149
150         if (disconnectOnApply() && !isClosing()) {
151                 disconnect();
152                 initialiseParams(string());
153                 updateView();
154         }
155 }
156
157
158 void Dialog::prepareView()
159 {
160         // Make sure the dialog controls are correctly enabled/disabled with
161         // readonly status.
162         checkStatus();
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
172
173 void Dialog::showView()
174 {
175         prepareView();
176
177         QWidget * w = asQWidget();
178         if (w->isVisible()) {
179                 w->raise();
180                 w->activateWindow();
181         } else
182                 w->show();
183
184         if (wantInitialFocus())
185                 w->setFocus();
186         else {
187                 lyxview_->raise();
188                 lyxview_->activateWindow();
189                 lyxview_->setFocus();
190         }
191 }
192
193
194 void Dialog::hideView()
195 {
196         QWidget * w = asQWidget();
197         if (!w->isVisible())
198                 return;
199         clearParams();
200         disconnect();
201         w->hide();
202 }
203
204
205 bool Dialog::isVisibleView() const
206 {
207         return asQWidget()->isVisible();
208 }
209
210
211 Inset const * Dialog::inset(InsetCode code) const
212 {
213         Inset * ins = bufferview()->cursor().innerInsetOfType(code);
214         if (!ins)
215                 ins = bufferview()->cursor().nextInset();
216         if (!ins || ins->lyxCode() != code)
217                 return 0;
218         return ins;
219 }
220
221
222 void Dialog::checkStatus()
223 {
224         // buffer independent dialogs are always active.
225         // This check allows us leave canApply unimplemented for some dialogs.
226         if (!isBufferDependent()) {
227                 updateView();
228                 return;
229         }
230
231         // deactivate the dialog if we have no buffer
232         if (!isBufferAvailable()) {
233                 enableView(false);
234                 return;
235         }
236
237         // check whether this dialog may be active
238         if (canApply()) {
239                 bool const readonly = isBufferReadonly();
240                 enableView(!readonly || canApplyToReadOnly());
241                 // refreshReadOnly() is too generous in _enabling_ widgets
242                 // update dialog to disable disabled widgets again
243
244                 updateView();
245         } else
246                 enableView(false);
247 }
248
249
250 QString Dialog::sessionKey() const
251 {
252         return "views/" + QString::number(lyxview_->id())
253                 + "/" + name();
254 }
255
256
257 void Dialog::saveSession() const
258 {
259         QSettings settings;
260         settings.setValue(sessionKey() + "/geometry", asQWidget()->saveGeometry());
261 }
262
263
264 void Dialog::restoreSession()
265 {
266         QSettings settings;
267         asQWidget()->restoreGeometry(
268                 settings.value(sessionKey() + "/geometry").toByteArray());
269 }
270
271 } // namespace frontend
272 } // namespace lyx