]> git.lyx.org Git - features.git/blobdiff - src/frontends/qt4/Dialog.cpp
More cleanup from Andre.
[features.git] / src / frontends / qt4 / Dialog.cpp
index 40b4f376321a75595b0810446833f9fa9132c305..2be31a4ffc4aff2343b140fab629afdfb53798f0 100644 (file)
 #include "Dialog.h"
 
 #include "GuiView.h"
+#include "qt_helpers.h"
 
 #include "Buffer.h"
 #include "FuncRequest.h"
 #include "FuncStatus.h"
 #include "LyXFunc.h"
 
+#include "support/debug.h"
+
+#include <QSettings>
+#include <QString>
+
+#include <boost/assert.hpp>
+
 #include <string>
 
+using namespace std;
+using namespace lyx::support;
+
 namespace lyx {
 namespace frontend {
 
 
-Dialog::Dialog(GuiView & lv, std::string const & name)
-       : lyxview_(&lv), name_(name.c_str())
+Dialog::Dialog(GuiView & lv, string const & name, QString const & title)
+       : name_(name), title_(title), lyxview_(&lv)
 {}
 
 
@@ -34,14 +45,15 @@ Dialog::~Dialog()
 {}
 
 
-std::string Dialog::name() const
+string const & Dialog::name() const
 {
        return name_;
 }
 
+
 bool Dialog::canApply() const
 {
-       FuncRequest const fr(getLfun(), name_);
+       FuncRequest const fr(getLfun(), from_ascii(name_));
        FuncStatus const fs(getStatus(fr));
        return fs.enabled();
 }
@@ -56,7 +68,7 @@ void Dialog::dispatch(FuncRequest const & fr) const
 
 void Dialog::updateDialog() const
 {
-       dispatch(FuncRequest(LFUN_DIALOG_UPDATE, name_));
+       dispatch(FuncRequest(LFUN_DIALOG_UPDATE, from_ascii(name_)));
 }
 
 
@@ -80,7 +92,7 @@ bool Dialog::isBufferReadonly() const
 }
 
 
-std::string const Dialog::bufferFilepath() const
+string const Dialog::bufferFilepath() const
 {
        return buffer().filePath();
 }
@@ -122,5 +134,148 @@ Buffer const & Dialog::buffer() const
        return *lyxview_->buffer();
 }
 
+
+void Dialog::showData(string const & data)
+{
+       if (isBufferDependent() && !isBufferAvailable())
+               return;
+
+       if (!initialiseParams(data)) {
+               LYXERR0("Dialog \"" << name()
+                       << "\" failed to translate the data string passed to show()");
+               return;
+       }
+
+       showView();
+}
+
+
+void Dialog::apply()
+{
+       if (isBufferDependent()) {
+               if (!isBufferAvailable() ||
+                   (isBufferReadonly() && !canApplyToReadOnly()))
+                       return;
+       }
+
+       applyView();
+       dispatchParams();
+
+       if (disconnectOnApply() && !isClosing()) {
+               disconnect();
+               initialiseParams(string());
+               updateView();
+       }
+}
+
+
+void Dialog::updateData(string const & data)
+{
+       if (isBufferDependent() && !isBufferAvailable())
+               return;
+
+       if (!initialiseParams(data)) {
+               LYXERR0("Dialog \"" << name()
+                      << "\" could not be initialized");
+               return;
+       }
+
+       updateView();
+}
+
+
+void Dialog::showView()
+{
+       // Make sure the dialog is up-to-date.
+       updateView(); 
+       // Make sure the dialog controls are correctly enabled/disabled with
+       // readonly status.
+       checkStatus();
+       if (exitEarly())
+               return;
+
+       QWidget * w = asQWidget();
+       w->setWindowTitle(title_);
+
+       QSize const hint = w->sizeHint();
+       if (hint.height() >= 0 && hint.width() >= 0)
+               w->setMinimumSize(hint);
+
+       if (w->isVisible()) {
+               w->raise();
+               w->activateWindow();
+       } else
+               w->show();
+
+       w->setFocus();
+}
+
+
+void Dialog::hideView()
+{
+       QWidget * w = asQWidget();
+       if (!w->isVisible())
+               return;
+       clearParams();
+       disconnect();
+       w->hide();
+}
+
+
+bool Dialog::isVisibleView() const
+{
+       return asQWidget()->isVisible();
+}
+
+
+void Dialog::checkStatus()
+{
+       // buffer independant dialogs are always active.
+       // This check allows us leave canApply unimplemented for some dialogs.
+       if (!isBufferDependent())
+               return;
+
+       // deactivate the dialog if we have no buffer
+       if (!isBufferAvailable()) {
+               enableView(false);
+               return;
+       }
+
+       // check whether this dialog may be active
+       if (canApply()) {
+               bool const readonly = isBufferReadonly();
+               enableView(!readonly);
+               // refreshReadOnly() is too generous in _enabling_ widgets
+               // update dialog to disable disabled widgets again
+
+               if (!readonly || canApplyToReadOnly())
+                       updateView();
+
+       } else
+               enableView(false);
+}
+
+
+QString Dialog::sessionKey() const
+{
+       return "view-" + QString::number(lyxview_->id())
+               + "/" + toqstr(name());
+}
+
+
+void Dialog::saveSession() const
+{
+       QSettings settings;
+       settings.setValue(sessionKey() + "/geometry", asQWidget()->saveGeometry());
+}
+
+
+void Dialog::restoreSession()
+{
+       QSettings settings;
+       asQWidget()->restoreGeometry(
+               settings.value(sessionKey() + "/geometry").toByteArray());
+}
+
 } // namespace frontend
 } // namespace lyx