]> git.lyx.org Git - lyx.git/blob - src/frontends/qt/InsertTableWidget.cpp
Properly uncheck insert table toolbutton
[lyx.git] / src / frontends / qt / InsertTableWidget.cpp
1 /**
2  * \file InsertTableWidget.cpp
3  *
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Edwin Leuven
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "InsertTableWidget.h"
15
16 #include "GuiView.h"
17 #include "qt_helpers.h"
18
19 // DispatchResult.h is needed by the windows compiler because lyx::dispatch
20 // returns a DispatchResult const reference. Gcc does not complain. Weird...
21 #include "DispatchResult.h"
22 #include "FuncStatus.h"
23 #include "FuncRequest.h"
24 #include "LyX.h"
25
26 #include <QMouseEvent>
27 #include <QPainter>
28 #include <QString>
29 #include <QToolTip>
30
31
32 namespace lyx {
33 namespace frontend {
34
35 InsertTableWidget::InsertTableWidget(QWidget * parent)
36         : QWidget(parent, Qt::Popup), colwidth_(15), rowheight_(15), minrows_(5), mincols_(5)
37 {
38         init();
39         setMouseTracking(true);
40 }
41
42
43 void InsertTableWidget::init()
44 {
45         rows_ = minrows_;
46         cols_ = mincols_;
47         bottom_ = 0;
48         right_ = 0;
49         underMouse_ = false;
50 }
51
52
53 void InsertTableWidget::show(bool show)
54 {
55         if (!show)
56                 return;
57
58         init();
59         resetGeometry();
60         setVisible(true);
61         // emit signal
62         visible(true);
63 }
64
65
66 void InsertTableWidget::hideEvent(QHideEvent * event)
67 {
68         QWidget::hideEvent(event);
69         visible(false);
70 }
71
72
73 void InsertTableWidget::resetGeometry()
74 {
75         QPoint p = parentWidget()->mapToGlobal(parentWidget()->geometry().bottomLeft());
76         setGeometry(p.x() - parentWidget()->pos().x(),
77                                 p.y() - parentWidget()->pos().y(),
78                                 cols_ * colwidth_ + 1, rows_ * rowheight_ + 1);
79 }
80
81
82 void InsertTableWidget::mouseMoveEvent(QMouseEvent * event)
83 {
84         // do this ourselves because when the mouse leaves the app
85         // we get an enter event (ie underMouse() is true)!!
86 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
87         underMouse_ = geometry().contains(event->globalPosition().toPoint());
88 #else
89         underMouse_ = geometry().contains(event->globalPos());
90 #endif
91         if (!underMouse_) {
92                 bottom_ = 0;
93                 right_ = 0;
94                 update();
95                 return;
96         }
97
98         int const r0 = right_;
99         int const b0 = bottom_;
100 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
101         right_ = event->position().x() / colwidth_ + 1;
102         bottom_ = event->position().y() / rowheight_ + 1;
103 #else
104         right_ = event->x() / colwidth_ + 1;
105         bottom_ = event->y() / rowheight_ + 1;
106 #endif
107
108         int const newrows = std::max(minrows_, bottom_ + 1);
109         if (rows_ != newrows) {
110                 rows_ = newrows;
111                 resetGeometry();
112         }
113
114         int const newcols = std::max(mincols_, right_ + 1);
115         if (cols_ != newcols) {
116                 cols_ = newcols;
117                 resetGeometry();
118         }
119
120         if (bottom_ != b0 || right_ != r0) {
121                 update();
122                 QString const status = QString("%1x%2").arg(bottom_).arg(right_);
123 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
124                 QToolTip::showText(event->globalPosition().toPoint(), status , this);
125 #else
126                 QToolTip::showText(event->globalPos(), status , this);
127 #endif
128         }
129 }
130
131
132 void InsertTableWidget::mouseReleaseEvent(QMouseEvent * /*event*/)
133 {
134         if (underMouse_) {
135                 QString const qdata = QString("%1 %2").arg(bottom_).arg(right_);
136                 lyx::dispatch(FuncRequest(LFUN_TABULAR_INSERT, fromqstr(qdata)));
137         }
138         // emit signal
139         visible(false);
140         close();
141 }
142
143
144 void InsertTableWidget::mousePressEvent(QMouseEvent * /*event*/)
145 {
146         // swallow this one
147 }
148
149
150 void InsertTableWidget::paintEvent(QPaintEvent * /*event*/)
151 {
152         QPalette const palette = this->palette();
153         drawGrid(rows_, cols_, palette.base(), palette.text().color());
154         if (underMouse_)
155                 drawGrid(bottom_, right_, palette.highlight(),
156                                 palette.highlightedText().color());
157 }
158
159
160 void InsertTableWidget::drawGrid(int const rows, int const cols,
161         QBrush const fillBrush, QColor lineColor)
162 {
163         QPainter painter(this);
164         painter.setPen(lineColor);
165         painter.setBrush(fillBrush);
166
167         for (int r = 0 ; r < rows ; ++r ) {
168                 for (int c = 0 ; c < cols ; ++c ) {
169                         QRect rectangle(c * colwidth_, r * rowheight_, colwidth_, rowheight_);
170                         painter.drawRect(rectangle);
171                 }
172         }
173 }
174
175
176 void InsertTableWidget::updateParent()
177 {
178         bool status = getStatus(FuncRequest(LFUN_TABULAR_INSERT)).enabled();
179         parentWidget()->setEnabled(status);
180 }
181
182
183 } // namespace frontend
184 } // namespace lyx
185
186 #include "moc_InsertTableWidget.cpp"