]> git.lyx.org Git - features.git/blob - src/frontends/qt/EmptyTable.cpp
Guard against possible referencing null.
[features.git] / src / frontends / qt / EmptyTable.cpp
1 /**
2  * \file EmptyTable.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Levon
7  * \author Jürgen Spitzmüller
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "EmptyTable.h"
15
16 #include "support/debug.h"
17
18 #include <QPainter>
19 #include <QMouseEvent>
20
21 /**
22  * A simple widget for a quick "preview" in TabularCreateDialog
23  */
24
25 EmptyTable::EmptyTable(QWidget * parent, int rows, int columns)
26         : QTableWidget(rows, columns, parent)
27 {
28         resetCellSize();
29         setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
30         setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
31         viewport()->resize(cellheight * rows, cellwidth * columns);
32         setSelectionMode(QAbstractItemView::NoSelection);
33         setFocusPolicy(Qt::NoFocus);
34         setEditTriggers(QAbstractItemView::NoEditTriggers);
35         adjustMinCellSize();
36 }
37
38
39 QSize EmptyTable::sizeHint() const
40 {
41         return QSize(cellwidth * (2 + columnCount()), cellheight * (2 + rowCount()));
42 }
43
44
45 void EmptyTable::adjustMinCellSize()
46 {
47         setRowHeight(0, cellheight);
48         cellheight = rowHeight(0);
49         setColumnWidth(0, cellwidth);
50         cellwidth = columnWidth(0);
51 }
52
53
54 void EmptyTable::resetCellSize()
55 {
56         for(int i = 0; i < rowCount(); ++i)
57                 setRowHeight(i, cellheight);
58         for(int i = 0; i < columnCount(); ++i)
59                 setColumnWidth(i, cellwidth);
60 }
61
62 void EmptyTable::paintCell(QPainter * p, int row, int col)
63 {
64         int const x2 = columnWidth(col) - 1;
65         int const y2 = rowHeight(row) - 1;
66
67         p->fillRect(0, 0, x2, y2, QColor("white"));
68         p->drawLine(x2, 0, x2, y2);
69         p->drawLine(0, y2, x2, y2);
70
71         if (row + 1 != rowCount() || col + 1 != columnCount())
72                 return;
73
74         // draw handle
75         int const step = cellheight / 5;
76         int const space = 4;
77         int x = cellwidth - step;
78         int const y = cellheight - space;
79         int const ex = cellwidth - space;
80         int ey = cellheight - step;
81         while (x > space) {
82                 p->drawLine(x, y, ex, ey);
83                 x -= step;
84                 ey -= step;
85         }
86 }
87
88
89 void EmptyTable::setNumberColumns(int nr_cols)
90 {
91         if (nr_cols < 1)
92                 return;
93         if (nr_cols == columnCount())
94                 return;
95         setColumnCount(nr_cols);
96         resetCellSize();
97         updateGeometry();
98         // emit signal
99         colsChanged(nr_cols);
100 }
101
102
103 void EmptyTable::setNumberRows(int nr_rows)
104 {
105         if (nr_rows < 1)
106                 return;
107         if (nr_rows == rowCount())
108                 return;
109         setRowCount(nr_rows);
110         resetCellSize();
111         updateGeometry();
112         // emit signal
113         rowsChanged(nr_rows);
114 }
115
116
117 void EmptyTable::mouseMoveEvent(QMouseEvent *ev)
118 {
119         int cc = columnCount();
120         int rc = rowCount();
121 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
122         int x = ev->position().x();
123         int y = ev->position().y();
124 #else
125         int x = ev->x();
126         int y = ev->y();
127 #endif
128         int w = cellwidth * cc;
129         int h = cellheight * rc;
130         int wl = cellwidth * (cc - 1);
131         int hl = cellheight * (rc - 1);
132         if (x > w)
133                 setNumberColumns(cc + 1);
134         if (y > h)
135                 setNumberRows(rc + 1);
136         if (x < wl)
137                 setNumberColumns(cc - 1);
138         if (y < hl)
139                 setNumberRows(rc - 1);
140 }
141
142 #include "moc_EmptyTable.cpp"
143
144
145 namespace lyx {
146
147
148 } // namespace lyx