]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/InsertTableWidget.C
more cleanup: some formatting and handle mouse events in their event handlers (and...
[lyx.git] / src / frontends / qt4 / InsertTableWidget.C
1 /**\r
2  * \file InsertTableWidget.C\r
3  *\r
4  * This file is part of LyX, the document processor.\r
5  * Licence details can be found in the file COPYING.\r
6  *\r
7  * \author Edwin Leuven\r
8  *\r
9  * Full author contact details are available in file CREDITS.\r
10  */\r
11 \r
12 #include <config.h>\r
13 \r
14 #include "BufferView.h" // needed for lyxfunc\r
15 #include "lyxfunc.h"\r
16 #include "FuncStatus.h"\r
17 #include "funcrequest.h"\r
18 #include "LyXView.h"\r
19 \r
20 #include "qt_helpers.h"\r
21 \r
22 #include "InsertTableWidget.h"\r
23 #include <QMouseEvent>\r
24 #include <QString>\r
25 #include <QToolTip>\r
26 #include <QPainter>\r
27 \r
28 \r
29 namespace lyx {\r
30 namespace frontend {\r
31 \r
32 InsertTableWidget::InsertTableWidget(LyXView & lyxView, QWidget * parent)\r
33         : QWidget(parent, Qt::Popup), colwidth_(20), rowheight_(12), lyxView_(lyxView)\r
34 {\r
35         init();\r
36         setMouseTracking(true);\r
37 }\r
38 \r
39 \r
40 void InsertTableWidget::init()\r
41 {\r
42         rows_ = 5;\r
43         cols_ = 5;\r
44         bottom_ = 0;\r
45         right_ = 0;\r
46         underMouse_ = false;\r
47 }\r
48 \r
49 \r
50 void InsertTableWidget::show(bool show)\r
51 {\r
52         if (!show)\r
53                 return;\r
54 \r
55         init();\r
56         resetGeometry();\r
57         setVisible(true);\r
58         emit visible(true);\r
59 }\r
60 \r
61 \r
62 void InsertTableWidget::resetGeometry()\r
63 {\r
64         QPoint p = parentWidget()->mapToGlobal(parentWidget()->geometry().bottomLeft());\r
65         setGeometry(p.x() - parentWidget()->pos().x(), \r
66                                 p.y() - parentWidget()->pos().y(),\r
67                                 cols_ * colwidth_ + 1, rows_ * rowheight_ + 1);\r
68 }\r
69 \r
70 \r
71 void InsertTableWidget::mouseMoveEvent(QMouseEvent * event)\r
72 {\r
73         // do this ourselves because when the mouse leaves the app\r
74         // we get an enter event (ie underMouse() is true)!!\r
75         underMouse_ = geometry().contains(event->globalPos());\r
76         if (!underMouse_) {\r
77                 bottom_ = 0;\r
78                 right_ = 0;\r
79                 update();\r
80                 return;\r
81         }\r
82 \r
83         int const r0 = right_;\r
84         int const b0 = bottom_;\r
85         right_ = event->x() / colwidth_ + 1;\r
86         bottom_ = event->y() / rowheight_ + 1;\r
87 \r
88         if (bottom_ == rows_) {\r
89                 ++rows_;\r
90                 resetGeometry();\r
91         }\r
92 \r
93         if (right_ == cols_) {\r
94                 ++cols_;\r
95                 resetGeometry();\r
96         }\r
97 \r
98         if (bottom_ != b0 || right_ != r0) {\r
99                 update();\r
100                 QString const status = QString("%1x%2").arg(bottom_).arg(right_);\r
101                 QToolTip::showText(event->globalPos(), status , this);\r
102         }\r
103 }\r
104 \r
105 \r
106 void InsertTableWidget::mouseReleaseEvent(QMouseEvent * event)\r
107 {\r
108         if (underMouse_) {\r
109                 QString const data = QString("%1 %2").arg(bottom_).arg(right_);\r
110                 lyxView_.getLyXFunc().dispatch(FuncRequest(LFUN_TABULAR_INSERT, fromqstr(data)));\r
111         }\r
112         emit visible(false);\r
113         close();\r
114 }\r
115 \r
116 \r
117 void InsertTableWidget::mousePressEvent(QMouseEvent * event)\r
118 {\r
119         // swallow this one\r
120 }\r
121 \r
122 \r
123 void InsertTableWidget::paintEvent(QPaintEvent * event)\r
124 {\r
125         drawGrid(rows_, cols_, Qt::white);\r
126         if (underMouse_)\r
127                 drawGrid(bottom_, right_, Qt::darkBlue);\r
128 }\r
129 \r
130 \r
131 void InsertTableWidget::drawGrid(int const rows, int const cols, Qt::GlobalColor const color)\r
132 {\r
133         QPainter painter(this);\r
134         painter.setPen(Qt::darkGray);\r
135         painter.setBrush(color);\r
136 \r
137         for (int r = 0 ; r < rows ; ++r ) {\r
138                 for (int c = 0 ; c < cols ; ++c ) {\r
139                         QRect rectangle(c * colwidth_, r * rowheight_, colwidth_, rowheight_);\r
140                         painter.drawRect(rectangle);\r
141                 }\r
142         }\r
143 }\r
144 \r
145 \r
146 void InsertTableWidget::updateParent()\r
147 {\r
148         bool status = lyxView_.getLyXFunc().getStatus(FuncRequest(LFUN_TABULAR_INSERT)).enabled();\r
149         parentWidget()->setEnabled(status);\r
150 }\r
151 \r
152 \r
153 } // namespace frontend\r
154 } // namespace lyx\r
155 \r
156 #include "InsertTableWidget_moc.cpp"\r