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();
177 if (w->isVisible()) {
183 if (wantInitialFocus())
187 lyxview_.activateWindow();
193 void Dialog::hideView()
195 QWidget * w = asQWidget();
204 bool Dialog::isVisibleView() const
206 return asQWidget()->isVisible();
210 Inset const * Dialog::inset(InsetCode code) const
212 // ins: the innermost inset of the type we look for
213 // that contains the cursor
214 Inset * ins = bufferview()->cursor().innerInsetOfType(code);
215 // next: a potential inset at cursor position
216 Inset * next = bufferview()->cursor().nextInset();
217 // Check if next is of the type we look for
219 if (next->lyxCode() != code)
222 // prefer next if it is of the requested type (bug 8716)
226 // no containing inset of requested type
227 // use next (which might also be 0)
233 void Dialog::checkStatus()
235 // buffer independent dialogs are always active.
236 // This check allows us leave canApply unimplemented for some dialogs.
237 if (!isBufferDependent()) {
242 // deactivate the dialog if we have no buffer
243 if (!isBufferAvailable()) {
248 // check whether this dialog may be active
250 bool const readonly = isBufferReadonly();
251 enableView(!readonly || canApplyToReadOnly());
258 QString Dialog::sessionKey() const
260 return "views/" + QString::number(lyxview_.id())
265 void Dialog::saveSession(QSettings & settings) const
267 settings.setValue(sessionKey() + "/geometry", asQWidget()->saveGeometry());
271 void Dialog::restoreSession()
274 asQWidget()->restoreGeometry(
275 settings.value(sessionKey() + "/geometry").toByteArray());
279 // If we have just created an inset, then we want to attach the
280 // dialog to it. This (i) allows further modification of that inset and
281 // (ii) prevents an additional click on Apply or OK from unexpectedly
282 // creating another inset. (See #3964 and #11030.)
283 void Dialog::connectToNewInset()
285 GuiView & view = const_cast<GuiView &>(lyxview());
286 BufferView * bv = view.currentBufferView();
287 // should have one, but just to be safe...
291 // are we attached to an inset already?
292 Inset * ins = bv->editedInset(fromqstr(name_));
296 // no, so we just inserted one, and now we are behind it.
297 Cursor const & cur = bv->cursor();
298 ins = cur.prevInset();
300 bv->editInset(fromqstr(name_), ins);
303 } // namespace frontend