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_(20), rowheight_(12)
39 setMouseTracking(true);
43 void InsertTableWidget::init()
53 void InsertTableWidget::show(bool show)
66 void InsertTableWidget::resetGeometry()
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);
75 void InsertTableWidget::mouseMoveEvent(QMouseEvent * event)
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());
82 underMouse_ = geometry().contains(event->globalPos());
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;
97 right_ = event->x() / colwidth_ + 1;
98 bottom_ = event->y() / rowheight_ + 1;
101 if (bottom_ == rows_) {
106 if (right_ == cols_) {
111 if (bottom_ != b0 || right_ != r0) {
113 QString const status = QString("%1x%2").arg(bottom_).arg(right_);
114 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
115 QToolTip::showText(event->globalPosition().toPoint(), status , this);
117 QToolTip::showText(event->globalPos(), status , this);
123 void InsertTableWidget::mouseReleaseEvent(QMouseEvent * /*event*/)
126 QString const qdata = QString("%1 %2").arg(bottom_).arg(right_);
127 lyx::dispatch(FuncRequest(LFUN_TABULAR_INSERT, fromqstr(qdata)));
135 void InsertTableWidget::mousePressEvent(QMouseEvent * /*event*/)
141 void InsertTableWidget::paintEvent(QPaintEvent * /*event*/)
143 drawGrid(rows_, cols_, Qt::white);
145 drawGrid(bottom_, right_, Qt::darkBlue);
149 void InsertTableWidget::drawGrid(int const rows, int const cols, Qt::GlobalColor const color)
151 QPainter painter(this);
152 painter.setPen(Qt::darkGray);
153 painter.setBrush(color);
155 for (int r = 0 ; r < rows ; ++r ) {
156 for (int c = 0 ; c < cols ; ++c ) {
157 QRect rectangle(c * colwidth_, r * rowheight_, colwidth_, rowheight_);
158 painter.drawRect(rectangle);
164 void InsertTableWidget::updateParent()
166 bool status = getStatus(FuncRequest(LFUN_TABULAR_INSERT)).enabled();
167 parentWidget()->setEnabled(status);
171 } // namespace frontend
174 #include "moc_InsertTableWidget.cpp"