]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/Dialog.cpp
Complete the removal of the embedding stuff. Maybe. It's hard to be sure we got every...
[lyx.git] / src / frontends / qt4 / Dialog.cpp
index f4f69928d2bb1ca427817e06ec604031a4e943e4..32c47c61295333612b41f78a9048a7e8bff65a16 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 "support/assert.h"
+
 #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)
+Dialog::Dialog(GuiView & lv, QString const & name, QString const & title)
+       : name_(name), title_(title), lyxview_(&lv)
 {}
 
 
@@ -34,14 +45,9 @@ Dialog::~Dialog()
 {}
 
 
-std::string const & Dialog::name() const
-{
-       return name_;
-}
-
 bool Dialog::canApply() const
 {
-       FuncRequest const fr(getLfun(), name_);
+       FuncRequest const fr(getLfun(), fromqstr(name_));
        FuncStatus const fs(getStatus(fr));
        return fs.enabled();
 }
@@ -56,13 +62,13 @@ void Dialog::dispatch(FuncRequest const & fr) const
 
 void Dialog::updateDialog() const
 {
-       dispatch(FuncRequest(LFUN_DIALOG_UPDATE, name_));
+       dispatch(FuncRequest(LFUN_DIALOG_UPDATE, fromqstr(name_)));
 }
 
 
 void Dialog::disconnect() const
 {
-       lyxview_->disconnectDialog(name_);
+       lyxview_->disconnectDialog(fromqstr(name_));
 }
 
 
@@ -80,9 +86,9 @@ bool Dialog::isBufferReadonly() const
 }
 
 
-std::string const Dialog::bufferFilepath() const
+QString Dialog::bufferFilepath() const
 {
-       return buffer().filePath();
+       return toqstr(buffer().filePath());
 }
 
 
@@ -111,16 +117,159 @@ BufferView const * Dialog::bufferview() const
 
 Buffer & Dialog::buffer()
 {
-       BOOST_ASSERT(lyxview_->buffer());
+       LASSERT(lyxview_->buffer(), /**/);
        return *lyxview_->buffer();
 }
 
 
 Buffer const & Dialog::buffer() const
 {
-       BOOST_ASSERT(lyxview_->buffer());
+       LASSERT(lyxview_->buffer(), /**/);
        return *lyxview_->buffer();
 }
 
+
+void Dialog::showData(string const & data)
+{
+       if (isBufferDependent() && !isBufferAvailable())
+               return;
+
+       if (!initialiseParams(data)) {
+               LYXERR0("Dialog \"" << fromqstr(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 \"" << fromqstr(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())
+               + "/" + 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