]> git.lyx.org Git - features.git/blob - src/frontends/qt/InsertTableWidget.cpp
InsertTableWidget support for dark mode and improved resizing
[features.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::resetGeometry()
67 {
68         QPoint p = parentWidget()->mapToGlobal(parentWidget()->geometry().bottomLeft());
69         setGeometry(p.x() - parentWidget()->pos().x(),
70                                 p.y() - parentWidget()->pos().y(),
71                                 cols_ * colwidth_ + 1, rows_ * rowheight_ + 1);
72 }
73
74
75 void InsertTableWidget::mouseMoveEvent(QMouseEvent * event)
76 {
77         // do this ourselves because when the mouse leaves the app
78         // we get an enter event (ie underMouse() is true)!!
79 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
80         underMouse_ = geometry().contains(event->globalPosition().toPoint());
81 #else
82         underMouse_ = geometry().contains(event->globalPos());
83 #endif
84         if (!underMouse_) {
85                 bottom_ = 0;
86                 right_ = 0;
87                 update();
88                 return;
89         }
90
91         int const r0 = right_;
92         int const b0 = bottom_;
93 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
94         right_ = event->position().x() / colwidth_ + 1;
95         bottom_ = event->position().y() / rowheight_ + 1;
96 #else
97         right_ = event->x() / colwidth_ + 1;
98         bottom_ = event->y() / rowheight_ + 1;
99 #endif
100
101         int const newrows = std::max(minrows_, bottom_ + 1);
102         if (rows_ != newrows) {
103                 rows_ = newrows;
104                 resetGeometry();
105         }
106
107         int const newcols = std::max(mincols_, right_ + 1);
108         if (cols_ != newcols) {
109                 cols_ = newcols;
110                 resetGeometry();
111         }
112
113         if (bottom_ != b0 || right_ != r0) {
114                 update();
115                 QString const status = QString("%1x%2").arg(bottom_).arg(right_);
116 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
117                 QToolTip::showText(event->globalPosition().toPoint(), status , this);
118 #else
119                 QToolTip::showText(event->globalPos(), status , this);
120 #endif
121         }
122 }
123
124
125 void InsertTableWidget::mouseReleaseEvent(QMouseEvent * /*event*/)
126 {
127         if (underMouse_) {
128                 QString const qdata = QString("%1 %2").arg(bottom_).arg(right_);
129                 lyx::dispatch(FuncRequest(LFUN_TABULAR_INSERT, fromqstr(qdata)));
130         }
131         // emit signal
132         visible(false);
133         close();
134 }
135
136
137 void InsertTableWidget::mousePressEvent(QMouseEvent * /*event*/)
138 {
139         // swallow this one
140 }
141
142
143 void InsertTableWidget::paintEvent(QPaintEvent * /*event*/)
144 {
145         QPalette const palette = this->palette();
146         drawGrid(rows_, cols_, palette.base(), palette.text().color());
147         if (underMouse_)
148                 drawGrid(bottom_, right_, palette.highlight(),
149                                 palette.highlightedText().color());
150 }
151
152
153 void InsertTableWidget::drawGrid(int const rows, int const cols,
154         QBrush const fillBrush, QColor lineColor)
155 {
156         QPainter painter(this);
157         painter.setPen(lineColor);
158         painter.setBrush(fillBrush);
159
160         for (int r = 0 ; r < rows ; ++r ) {
161                 for (int c = 0 ; c < cols ; ++c ) {
162                         QRect rectangle(c * colwidth_, r * rowheight_, colwidth_, rowheight_);
163                         painter.drawRect(rectangle);
164                 }
165         }
166 }
167
168
169 void InsertTableWidget::updateParent()
170 {
171         bool status = getStatus(FuncRequest(LFUN_TABULAR_INSERT)).enabled();
172         parentWidget()->setEnabled(status);
173 }
174
175
176 } // namespace frontend
177 } // namespace lyx
178
179 #include "moc_InsertTableWidget.cpp"