2 * \file InsertTableWidget.cpp
4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
9 * Full author contact details are available in file CREDITS.
14 #include "InsertTableWidget.h"
17 #include "qt_helpers.h"
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"
26 #include <QMouseEvent>
35 InsertTableWidget::InsertTableWidget(QWidget * parent)
36 : QWidget(parent, Qt::Popup), colwidth_(15), rowheight_(15), minrows_(5), mincols_(5)
39 setMouseTracking(true);
40 // display tooltip without any delay
41 setStyle(new ProxyStyle(style()));
45 void InsertTableWidget::init()
55 void InsertTableWidget::show(bool show)
68 void InsertTableWidget::hideEvent(QHideEvent * event)
70 QWidget::hideEvent(event);
75 void InsertTableWidget::resetGeometry()
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);
84 void InsertTableWidget::mouseMoveEvent(QMouseEvent * event)
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());
91 underMouse_ = geometry().contains(event->globalPos());
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;
106 right_ = event->x() / colwidth_ + 1;
107 bottom_ = event->y() / rowheight_ + 1;
110 int const newrows = std::max(minrows_, bottom_ + 1);
111 if (rows_ != newrows) {
116 int const newcols = std::max(mincols_, right_ + 1);
117 if (cols_ != newcols) {
122 if (bottom_ != b0 || right_ != r0) {
124 QString const status = QString("%1x%2").arg(bottom_).arg(right_);
130 void InsertTableWidget::mouseReleaseEvent(QMouseEvent * /*event*/)
133 QString const qdata = QString("%1 %2").arg(bottom_).arg(right_);
134 lyx::dispatch(FuncRequest(LFUN_TABULAR_INSERT, fromqstr(qdata)));
142 void InsertTableWidget::mousePressEvent(QMouseEvent * /*event*/)
148 void InsertTableWidget::paintEvent(QPaintEvent * /*event*/)
150 QPalette const palette = this->palette();
151 drawGrid(rows_, cols_, palette.base(), palette.text().color());
153 drawGrid(bottom_, right_, palette.highlight(),
154 palette.highlightedText().color());
158 void InsertTableWidget::drawGrid(int const rows, int const cols,
159 QBrush const fillBrush, QColor lineColor)
161 QPainter painter(this);
162 painter.setPen(lineColor);
163 painter.setBrush(fillBrush);
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);
174 void InsertTableWidget::updateParent()
176 bool status = getStatus(FuncRequest(LFUN_TABULAR_INSERT)).enabled();
177 parentWidget()->setEnabled(status);
181 } // namespace frontend
184 #include "moc_InsertTableWidget.cpp"