]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/InsertTableWidget.cpp
Support for command argument placement after the workarea argument
[lyx.git] / src / frontends / qt4 / 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 #include "FuncStatus.h"
20 #include "FuncRequest.h"
21 #include "LyX.h"
22
23 #include <QMouseEvent>
24 #include <QPainter>
25 #include <QString>
26 #include <QToolTip>
27
28
29 namespace lyx {
30 namespace frontend {
31
32 InsertTableWidget::InsertTableWidget(GuiView & lyxView, QWidget * parent)
33         : QWidget(parent, Qt::Popup), colwidth_(20), rowheight_(12), lyxView_(lyxView)
34 {
35         init();
36         setMouseTracking(true);
37 }
38
39
40 void InsertTableWidget::init()
41 {
42         rows_ = 5;
43         cols_ = 5;
44         bottom_ = 0;
45         right_ = 0;
46         underMouse_ = false;
47 }
48
49
50 void InsertTableWidget::show(bool show)
51 {
52         if (!show)
53                 return;
54
55         init();
56         resetGeometry();
57         setVisible(true);
58         // emit signal
59         visible(true);
60 }
61
62
63 void InsertTableWidget::resetGeometry()
64 {
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);
69 }
70
71
72 void InsertTableWidget::mouseMoveEvent(QMouseEvent * event)
73 {
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());
77         if (!underMouse_) {
78                 bottom_ = 0;
79                 right_ = 0;
80                 update();
81                 return;
82         }
83
84         int const r0 = right_;
85         int const b0 = bottom_;
86         right_ = event->x() / colwidth_ + 1;
87         bottom_ = event->y() / rowheight_ + 1;
88
89         if (bottom_ == rows_) {
90                 ++rows_;
91                 resetGeometry();
92         }
93
94         if (right_ == cols_) {
95                 ++cols_;
96                 resetGeometry();
97         }
98
99         if (bottom_ != b0 || right_ != r0) {
100                 update();
101                 QString const status = QString("%1x%2").arg(bottom_).arg(right_);
102                 QToolTip::showText(event->globalPos(), status , this);
103         }
104 }
105
106
107 void InsertTableWidget::mouseReleaseEvent(QMouseEvent * /*event*/)
108 {
109         if (underMouse_) {
110                 QString const data = QString("%1 %2").arg(bottom_).arg(right_);
111                 lyx::dispatch(FuncRequest(LFUN_TABULAR_INSERT, fromqstr(data)));
112         }
113         // emit signal
114         visible(false);
115         close();
116 }
117
118
119 void InsertTableWidget::mousePressEvent(QMouseEvent * /*event*/)
120 {
121         // swallow this one
122 }
123
124
125 void InsertTableWidget::paintEvent(QPaintEvent * /*event*/)
126 {
127         drawGrid(rows_, cols_, Qt::white);
128         if (underMouse_)
129                 drawGrid(bottom_, right_, Qt::darkBlue);
130 }
131
132
133 void InsertTableWidget::drawGrid(int const rows, int const cols, Qt::GlobalColor const color)
134 {
135         QPainter painter(this);
136         painter.setPen(Qt::darkGray);
137         painter.setBrush(color);
138
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);
143                 }
144         }
145 }
146
147
148 void InsertTableWidget::updateParent()
149 {
150         bool status = getStatus(FuncRequest(LFUN_TABULAR_INSERT)).enabled();
151         parentWidget()->setEnabled(status);
152 }
153
154
155 } // namespace frontend
156 } // namespace lyx
157
158 #include "moc_InsertTableWidget.cpp"