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())
109 // This case should not happen.
114 BufferView const * Dialog::bufferview() const
116 return lyxview_.currentBufferView();
120 Buffer const & Dialog::buffer() const
122 LAPPERR(lyxview_.currentBufferView());
123 return lyxview_.currentBufferView()->buffer();
127 Buffer const & Dialog::documentBuffer() const
129 LAPPERR(lyxview_.documentBufferView());
130 return lyxview_.documentBufferView()->buffer();
134 void Dialog::showData(string const & data)
136 if (isBufferDependent() && !isBufferAvailable())
139 if (!initialiseParams(data)) {
140 LYXERR0("Dialog \"" << name()
141 << "\" failed to translate the data string passed to show()");
151 if (isBufferDependent()) {
152 if (!isBufferAvailable() ||
153 (isBufferReadonly() && !canApplyToReadOnly()))
160 if (disconnectOnApply() && !isClosing()) {
162 initialiseParams(string());
168 void Dialog::prepareView()
170 // Make sure the dialog controls are correctly enabled/disabled with
174 QWidget * w = asQWidget();
175 w->setWindowTitle(title_);
177 QSize const hint = w->sizeHint();
178 if (hint.height() >= 0 && hint.width() >= 0)
179 w->setMinimumSize(hint);
183 void Dialog::showView()
187 QWidget * w = asQWidget();
188 if (w->isVisible()) {
194 if (wantInitialFocus())
198 lyxview_.activateWindow();
204 void Dialog::hideView()
206 QWidget * w = asQWidget();
215 bool Dialog::isVisibleView() const
217 return asQWidget()->isVisible();
221 Inset const * Dialog::inset(InsetCode code) const
223 // ins: the innermost inset of the type we look for
224 // that contains the cursor
225 Inset * ins = bufferview()->cursor().innerInsetOfType(code);
226 // next: a potential inset at cursor position
227 Inset * next = bufferview()->cursor().nextInset();
228 // Check if next is of the type we look for
230 if (next->lyxCode() != code)
233 // prefer next if it is of the requested type (bug 8716)
237 // no containing inset of requested type
238 // use next (which might also be 0)
244 void Dialog::checkStatus()
246 // buffer independent dialogs are always active.
247 // This check allows us leave canApply unimplemented for some dialogs.
248 if (!isBufferDependent()) {
253 // deactivate the dialog if we have no buffer
254 if (!isBufferAvailable()) {
259 // check whether this dialog may be active
261 bool const readonly = isBufferReadonly();
262 enableView(!readonly || canApplyToReadOnly());
269 QString Dialog::sessionKey() const
271 return "views/" + QString::number(lyxview_.id())
276 void Dialog::saveSession(QSettings & settings) const
278 settings.setValue(sessionKey() + "/geometry", asQWidget()->saveGeometry());
282 void Dialog::restoreSession()
285 asQWidget()->restoreGeometry(
286 settings.value(sessionKey() + "/geometry").toByteArray());
289 } // namespace frontend