3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Angus Leeming
8 * Full author contact details are available in file CREDITS.
16 #include "qt_helpers.h"
19 #include "BufferParams.h"
20 #include "BufferView.h"
22 #include "FuncRequest.h"
23 #include "FuncStatus.h"
26 #include "insets/Inset.h"
28 #include "support/debug.h"
29 #include "support/gettext.h"
30 #include "support/lassert.h"
42 using namespace lyx::support;
47 Dialog::Dialog(GuiView & lv, QString const & name, QString const & title)
48 : name_(name), title_(title), lyxview_(lv)
56 bool Dialog::canApply() const
58 FuncRequest const fr(getLfun(), fromqstr(name_));
59 FuncStatus const fs(getStatus(fr));
64 void Dialog::dispatch(FuncRequest const & fr) const
70 void Dialog::updateDialog() const
72 dispatch(FuncRequest(LFUN_DIALOG_UPDATE, fromqstr(name_)));
76 void Dialog::disconnect() const
78 lyxview_.disconnectDialog(fromqstr(name_));
82 bool Dialog::isBufferAvailable() const
84 return lyxview_.currentBufferView() != nullptr;
88 bool Dialog::isBufferReadonly() const
90 if (!lyxview_.documentBufferView())
92 return lyxview_.documentBufferView()->buffer().isReadonly();
96 QString Dialog::bufferFilePath() const
98 return toqstr(buffer().filePath());
102 KernelDocType Dialog::docType() const
104 if (buffer().params().isLatex())
106 if (buffer().params().isLiterate())
113 BufferView const * Dialog::bufferview() const
115 return lyxview_.currentBufferView();
119 Buffer const & Dialog::buffer() const
121 LAPPERR(lyxview_.currentBufferView());
122 return lyxview_.currentBufferView()->buffer();
126 Buffer const & Dialog::documentBuffer() const
128 LAPPERR(lyxview_.documentBufferView());
129 return lyxview_.documentBufferView()->buffer();
133 void Dialog::showData(string const & data)
135 if (isBufferDependent() && !isBufferAvailable())
138 if (!initialiseParams(data)) {
139 LYXERR0("Dialog \"" << name()
140 << "\" failed to translate the data string passed to show()");
150 if (isBufferDependent()) {
151 if (!isBufferAvailable() ||
152 (isBufferReadonly() && !canApplyToReadOnly()))
159 if (disconnectOnApply() && !isClosing()) {
161 initialiseParams(string());
167 void Dialog::prepareView()
169 // Make sure the dialog controls are correctly enabled/disabled with
173 QWidget * w = asQWidget();
174 w->setWindowTitle(title_);
176 QSize const hint = w->sizeHint();
177 if (hint.height() >= 0 && hint.width() >= 0)
178 w->setMinimumSize(hint);
182 void Dialog::showView()
186 QWidget * w = asQWidget();
187 if (w->isVisible()) {
193 if (wantInitialFocus())
197 lyxview_.activateWindow();
203 void Dialog::hideView()
205 QWidget * w = asQWidget();
214 bool Dialog::isVisibleView() const
216 return asQWidget()->isVisible();
220 Inset const * Dialog::inset(InsetCode code) const
222 // ins: the innermost inset of the type we look for
223 // that contains the cursor
224 Inset * ins = bufferview()->cursor().innerInsetOfType(code);
225 // next: a potential inset at cursor position
226 Inset * next = bufferview()->cursor().nextInset();
227 // Check if next is of the type we look for
229 if (next->lyxCode() != code)
232 // prefer next if it is of the requested type (bug 8716)
236 // no containing inset of requested type
237 // use next (which might also be 0)
243 void Dialog::checkStatus()
245 // buffer independent dialogs are always active.
246 // This check allows us leave canApply unimplemented for some dialogs.
247 if (!isBufferDependent()) {
252 // deactivate the dialog if we have no buffer
253 if (!isBufferAvailable()) {
258 // check whether this dialog may be active
260 bool const readonly = isBufferReadonly();
261 enableView(!readonly || canApplyToReadOnly());
268 QString Dialog::sessionKey() const
270 return "views/" + QString::number(lyxview_.id())
275 void Dialog::saveSession(QSettings & settings) const
277 settings.setValue(sessionKey() + "/geometry", asQWidget()->saveGeometry());
281 void Dialog::restoreSession()
284 asQWidget()->restoreGeometry(
285 settings.value(sessionKey() + "/geometry").toByteArray());
288 } // namespace frontend