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 "support/debug.h"
27 #include "support/gettext.h"
28 #include "support/lassert.h"
35 using namespace lyx::support;
40 Dialog::Dialog(GuiView & lv, QString const & name, QString const & title)
41 : name_(name), title_(title), lyxview_(lv)
45 bool Dialog::canApply() const
47 FuncRequest const fr(getLfun(), fromqstr(name_));
48 FuncStatus const fs(getStatus(fr));
53 void Dialog::dispatch(FuncRequest const & fr) const
59 void Dialog::updateDialog() const
61 dispatch(FuncRequest(LFUN_DIALOG_UPDATE, fromqstr(name_)));
65 void Dialog::disconnect() const
67 lyxview_.disconnectDialog(fromqstr(name_));
71 bool Dialog::isBufferAvailable() const
73 return lyxview_.currentBufferView() != nullptr;
77 bool Dialog::isBufferReadonly() const
79 if (!lyxview_.documentBufferView())
81 return lyxview_.documentBufferView()->buffer().isReadonly();
85 QString Dialog::bufferFilePath() const
87 return toqstr(buffer().filePath());
91 KernelDocType Dialog::docType() const
93 if (buffer().params().isLatex())
94 return KernelDocType::LaTeX;
95 if (buffer().params().isLiterate())
96 return KernelDocType::Literate;
98 // This case should not happen.
99 return KernelDocType::LaTeX;
103 BufferView const * Dialog::bufferview() const
105 return lyxview_.currentBufferView();
109 Buffer const & Dialog::buffer() const
111 LAPPERR(lyxview_.currentBufferView());
112 return lyxview_.currentBufferView()->buffer();
116 Buffer const & Dialog::documentBuffer() const
118 LAPPERR(lyxview_.documentBufferView());
119 return lyxview_.documentBufferView()->buffer();
123 void Dialog::showData(string const & data)
125 if (isBufferDependent() && !isBufferAvailable())
128 if (!initialiseParams(data)) {
129 LYXERR0("Dialog \"" << name()
130 << "\" failed to translate the data string passed to show()");
140 if (isBufferDependent()) {
141 if (!isBufferAvailable() ||
142 (isBufferReadonly() && !canApplyToReadOnly()))
149 if (disconnectOnApply() && !isClosing()) {
151 initialiseParams(string());
157 void Dialog::prepareView()
159 // Make sure the dialog controls are correctly enabled/disabled with
163 QWidget * w = asQWidget();
164 w->setWindowTitle(title_);
166 QSize const hint = w->sizeHint();
167 if (hint.height() >= 0 && hint.width() >= 0)
168 w->setMinimumSize(hint);
172 void Dialog::showView()
176 QWidget * w = asQWidget();
181 if (wantInitialFocus())
185 lyxview_.activateWindow();
191 void Dialog::hideView()
193 QWidget * w = asQWidget();
202 bool Dialog::isVisibleView() const
204 return asQWidget()->isVisible();
208 Inset const * Dialog::inset(InsetCode code) const
210 // ins: the innermost inset of the type we look for
211 // that contains the cursor
212 Inset * ins = bufferview()->cursor().innerInsetOfType(code);
213 // next: a potential inset at cursor position
214 Inset * next = bufferview()->cursor().nextInset();
215 // Check if next is of the type we look for
217 if (next->lyxCode() != code)
220 // prefer next if it is of the requested type (bug 8716)
224 // no containing inset of requested type
225 // use next (which might also be 0)
231 void Dialog::checkStatus()
233 // buffer independent dialogs are always active.
234 // This check allows us leave canApply unimplemented for some dialogs.
235 if (!isBufferDependent()) {
240 // deactivate the dialog if we have no buffer
241 if (!isBufferAvailable()) {
246 // check whether this dialog may be active
248 bool const readonly = isBufferReadonly();
249 enableView(!readonly || canApplyToReadOnly());
256 QString Dialog::sessionKey() const
258 return "views/" + QString::number(lyxview_.id())
263 void Dialog::saveSession(QSettings & settings) const
265 settings.setValue(sessionKey() + "/geometry", asQWidget()->saveGeometry());
269 void Dialog::restoreSession()
272 asQWidget()->restoreGeometry(
273 settings.value(sessionKey() + "/geometry").toByteArray());
277 // If we have just created an inset, then we want to attach the
278 // dialog to it. This (i) allows further modification of that inset and
279 // (ii) prevents an additional click on Apply or OK from unexpectedly
280 // creating another inset. (See #3964 and #11030.)
281 void Dialog::connectToNewInset()
283 GuiView & view = const_cast<GuiView &>(lyxview());
284 BufferView * bv = view.currentBufferView();
285 // should have one, but just to be safe...
289 // are we attached to an inset already?
290 Inset * ins = bv->editedInset(fromqstr(name_));
294 // no, so we just inserted one, and now we are behind it.
295 Cursor const & cur = bv->cursor();
296 ins = cur.prevInset();
298 bv->editInset(fromqstr(name_), ins);
301 } // namespace frontend