]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QMathDialog.C
amortise panel load times
[lyx.git] / src / frontends / qt2 / QMathDialog.C
1 /**
2  * \file QMathDialog.C
3  * Copyright 2001 the LyX Team
4  * Read the file COPYING
5  *
6  * \author John Levon <moz@compsoc.man.ac.uk>
7  */
8
9 #include <config.h>
10
11 #include "support/filetools.h"
12 #include "debug.h"
13  
14 #include "QMathDialog.h"
15 #include "QMath.h"
16
17 #include "ControlMath.h"
18 #include "iconpalette.h"
19  
20 #include <qapplication.h>
21 #include <qwidgetstack.h>
22 #include <qcombobox.h>
23 #include <qlistbox.h>
24 #include <qpixmap.h>
25 #include <qscrollview.h>
26 #include <qlayout.h>
27  
28 using std::min;
29 using std::max;
30 using std::endl;
31  
32 class QScrollViewSingle : public QScrollView {
33 public:
34         QScrollViewSingle(QWidget * p)
35                 : QScrollView(p), w_(0) {
36                 setResizePolicy(Manual);
37                 setHScrollBarMode(AlwaysOff);
38                 setVScrollBarMode(AlwaysOn);
39                 setBackgroundMode(PaletteBackground);
40                 viewport()->setBackgroundMode(PaletteBackground);
41         }
42  
43         void setChild(QWidget * w) {
44                 w_ = w; 
45                 setMinimumWidth(verticalScrollBar()->width() + w_->width() + 4);
46                 addChild(w_);
47         }
48
49 protected:
50         virtual void resizeEvent(QResizeEvent * e) {
51                 QScrollView::resizeEvent(e);
52                 if (!w_)
53                         return;
54  
55                 w_->resize(viewport()->width(), w_->height());
56                 // force the resize to get accurate scrollbars
57                 qApp->processEvents();
58                 resizeContents(w_->width(), w_->height());
59         }
60
61 private:
62         QWidget * w_; 
63 };
64  
65 namespace { 
66         char const ** panels[] = {
67                 latex_bop, latex_varsz, latex_brel, latex_greek, latex_arrow,
68                 latex_dots, latex_deco, latex_misc, latex_ams_ops,
69                 latex_ams_rel, latex_ams_nrel, latex_ams_arrows,
70                 latex_ams_misc
71         };
72         int const nr_panels = sizeof(panels)/sizeof(panels[0]);
73
74 bool panel_initialised[nr_panels];
75 }
76  
77  
78 QMathDialog::QMathDialog(QMath * form)
79         : QMathDialogBase(0, 0, false, 0),
80         form_(form)
81 {
82         connect(symbolsCO, SIGNAL(activated(int)), symbolsWS, SLOT(raiseWidget(int))); 
83
84         for (int i = 0; *function_names[i]; ++i) {
85                 functionsLB->insertItem(function_names[i]);
86         }
87  
88         for (int i = 0; i < nr_panels; ++i) { 
89                 QScrollViewSingle * view = new QScrollViewSingle(symbolsWS);
90                 symbolsWS->addWidget(view, i);
91         }
92
93         // aboutToShow() only fires when != 0 in Qt 2 !
94         symbolsWS->raiseWidget(0);
95         addPanel(0);
96         panel_initialised[0] = true;
97  
98         connect(symbolsWS, SIGNAL(aboutToShow(int)), this, SLOT(showingPanel(int)));
99 }
100
101
102 void QMathDialog::showingPanel(int num)
103 {
104         if (panel_initialised[num])
105                 return;
106
107         addPanel(num);
108         panel_initialised[num] = true;
109 }
110
111  
112 IconPalette * QMathDialog::makePanel(QWidget * parent, char const ** entries)
113 {
114         IconPalette * p = new IconPalette(parent);
115         for (int i = 0; *entries[i]; ++i) {
116                 string xpm_name = LibFileSearch("images/math/", entries[i], "xpm");
117                 p->add(QPixmap(xpm_name.c_str()), entries[i], string("\\") + entries[i]);
118         }
119         connect(p, SIGNAL(button_clicked(string)), this, SLOT(symbol_clicked(string)));
120  
121         return p;
122 }
123
124  
125 void QMathDialog::addPanel(int num)
126 {
127         QScrollViewSingle * view = static_cast<QScrollViewSingle*>(symbolsWS->visibleWidget());
128         IconPalette * p = makePanel(view->viewport(), panels[num]);
129         view->setChild(p);
130 }
131
132  
133 void QMathDialog::symbol_clicked(string str)
134 {
135         form_->insert_symbol(str);
136 }
137
138  
139 void QMathDialog::fracClicked()
140 {
141         form_->insert_symbol("frac");
142 }
143  
144
145 void QMathDialog::sqrtClicked()
146 {
147         form_->insert_symbol("sqrt");
148 }
149
150  
151 void QMathDialog::delimiterClicked()
152 {
153 }
154
155  
156 void QMathDialog::expandClicked()
157 {
158         int const id = symbolsWS->id(symbolsWS->visibleWidget());
159         IconPalette * p = makePanel(0, panels[id]);
160         string s = "LyX: "; 
161         s += symbolsCO->text(id).latin1();
162         p->setCaption(s.c_str());
163         p->resize(40 * 5, p->height());
164         p->show();
165         p->setMaximumSize(p->width(), p->height());
166 }
167  
168  
169 void QMathDialog::functionSelected(const QString & str)
170 {
171         form_->insert_symbol(str.latin1()); 
172 }
173
174  
175 void QMathDialog::matrixClicked()
176 {
177 }
178
179  
180 void QMathDialog::spaceClicked()
181 {
182 }
183
184  
185 void QMathDialog::styleClicked()
186 {
187 }
188
189  
190 void QMathDialog::subscriptClicked()
191 {
192         form_->subscript();
193 }
194
195  
196 void QMathDialog::superscriptClicked()
197 {
198         form_->superscript();
199 }