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