]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/DialogView.h
Simplify Dialog::name() handling.
[lyx.git] / src / frontends / qt4 / DialogView.h
1 // -*- C++ -*-
2 /**
3  * \file DialogView.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Abdelrazak Younes
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #ifndef DIALOG_VIEW_H
13 #define DIALOG_VIEW_H
14
15 #include "Dialog.h"
16 #include "GuiView.h"
17 #include "qt_helpers.h"
18 #include "debug.h"
19
20 #include <QCloseEvent>
21 #include <QDialog>
22 #include <QSettings>
23 #include <QShowEvent>
24 #include <QGridLayout>
25
26 #include <string>
27
28 namespace lyx {
29 namespace frontend {
30
31 /// Window Dialog container for LyX dialogs.
32 /// This template class that encapsulates a given Widget inside a
33 /// QDialog and presents a Dialog interface
34 template<class MyWidget>
35 class DialogView : public QDialog, public Dialog
36 {
37 public:
38         DialogView(
39                 GuiView & parent, ///< the main window where to dock.
40                 std::string const & name, ///< dialog identifier.
41                 bool modal = false, ///< Window modality.
42                 Qt::WindowFlags flags = 0
43                 )
44                 : QDialog(&parent, flags), Dialog(parent, name)
45         {
46                 setModal(modal);
47                 QGridLayout * gridLayout = new QGridLayout(this);
48                 gridLayout->setMargin(0);
49                 widget_ = new MyWidget(*this, this);
50                 gridLayout->addWidget(widget_);
51                 setWindowTitle("LyX: " + widget_->windowTitle());
52         }
53
54         /// Dialog inherited methods
55         //@{
56         void applyView() {}
57         void hideView()
58         {
59                 clearParams();
60                 QDialog::hide();
61         }
62         void showData(std::string const & data)
63         {
64                 initialiseParams(data);
65                 showView();
66         }
67         void showView()
68         {
69                 widget_->updateView();  // make sure its up-to-date
70                 QDialog::show();
71                 raise();
72                 activateWindow();
73         }
74         bool isVisibleView() const { return QDialog::isVisible(); }
75         void checkStatus() { updateView(); }
76         void updateData(std::string const & data)
77         {
78                 initialiseParams(data);
79                 updateView();
80         }
81         void updateView()
82         {
83                 widget_->updateView();
84         }
85         //@}
86 private:
87         /// The encapsulated widget.
88         MyWidget * widget_;
89
90         void showEvent(QShowEvent * e)
91         {
92                 QSettings settings;
93                 std::string key = name() + "/geometry";
94                 QDialog::restoreGeometry(settings.value(key.c_str()).toByteArray());
95             QDialog::showEvent(e);
96         }
97
98         void closeEvent(QCloseEvent * e)
99         {
100                 QSettings settings;
101                 std::string key = name() + "/geometry";
102                 settings.setValue(key.c_str(), QDialog::saveGeometry());
103                 QDialog::closeEvent(e);
104         }
105 };
106
107 } // frontend
108 } // lyx
109
110 #endif // DIALOG_VIEW_H