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