2 * \file InsertTableWidget.C
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 "BufferView.h" // needed for lyxfunc
16 #include "FuncStatus.h"
17 #include "funcrequest.h"
20 #include "qt_helpers.h"
22 #include "InsertTableWidget.h"
23 #include <QMouseEvent>
32 InsertTableWidget::InsertTableWidget(LyXView & lyxView, QWidget * parent)
33 : QWidget(parent, Qt::Popup), colwidth_(20), rowheight_(12), lyxView_(lyxView)
36 setMouseTracking(true);
40 void InsertTableWidget::init()
50 void InsertTableWidget::show(bool show)
63 void InsertTableWidget::resetGeometry()
65 QPoint p = parentWidget()->mapToGlobal(parentWidget()->geometry().bottomLeft());
66 setGeometry(p.x() - parentWidget()->pos().x(),
67 p.y() - parentWidget()->pos().y(),
68 cols_ * colwidth_ + 1, rows_ * rowheight_ + 1);
72 void InsertTableWidget::mouseMoveEvent(QMouseEvent * event)
74 // do this ourselves because when the mouse leaves the app
75 // we get an enter event (ie underMouse() is true)!!
76 underMouse_ = geometry().contains(event->globalPos());
84 int const r0 = right_;
85 int const b0 = bottom_;
86 right_ = event->x() / colwidth_ + 1;
87 bottom_ = event->y() / rowheight_ + 1;
89 if (bottom_ == rows_) {
94 if (right_ == cols_) {
99 if (bottom_ != b0 || right_ != r0) {
101 QString const status = QString("%1x%2").arg(bottom_).arg(right_);
102 QToolTip::showText(event->globalPos(), status , this);
107 void InsertTableWidget::mouseReleaseEvent(QMouseEvent * /*event*/)
110 QString const data = QString("%1 %2").arg(bottom_).arg(right_);
111 lyxView_.dispatch(FuncRequest(LFUN_TABULAR_INSERT, fromqstr(data)));
119 void InsertTableWidget::mousePressEvent(QMouseEvent * /*event*/)
125 void InsertTableWidget::paintEvent(QPaintEvent * /*event*/)
127 drawGrid(rows_, cols_, Qt::white);
129 drawGrid(bottom_, right_, Qt::darkBlue);
133 void InsertTableWidget::drawGrid(int const rows, int const cols, Qt::GlobalColor const color)
135 QPainter painter(this);
136 painter.setPen(Qt::darkGray);
137 painter.setBrush(color);
139 for (int r = 0 ; r < rows ; ++r ) {
140 for (int c = 0 ; c < cols ; ++c ) {
141 QRect rectangle(c * colwidth_, r * rowheight_, colwidth_, rowheight_);
142 painter.drawRect(rectangle);
148 void InsertTableWidget::updateParent()
150 bool status = lyx::getStatus(FuncRequest(LFUN_TABULAR_INSERT)).enabled();
151 parentWidget()->setEnabled(status);
155 } // namespace frontend
158 #include "InsertTableWidget_moc.cpp"