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