]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/EmptyTable.cpp
Transfer some more dialog related code from core to frontend:
[lyx.git] / src / frontends / qt4 / 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  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "EmptyTable.h"
14
15 #include <QPainter>
16 #include <QMouseEvent>
17
18 /**
19  * A simple widget for a quick "preview" in TabularCreateDialog
20  */
21
22 unsigned int const cellsize = 20;
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(cellsize*rows,cellsize*columns);
32 }
33
34
35 QSize EmptyTable::sizeHint() const
36 {
37         return QSize(cellsize * (2+columnCount()), cellsize * (2+rowCount()));
38 }
39
40 void EmptyTable::resetCellSize()
41 {
42         for(int i=0; i<rowCount(); ++i)
43                 setRowHeight(i, cellsize);
44         for(int i=0; i<columnCount(); ++i)
45                 setColumnWidth(i, cellsize);
46 }
47
48 void EmptyTable::paintCell(QPainter * p, int row, int col)
49 {
50         int const x2 = columnWidth(col) - 1;
51         int const y2 = rowHeight(row) - 1;
52
53         p->fillRect(0, 0, x2, y2, QColor("white"));
54         p->drawLine(x2, 0, x2, y2);
55         p->drawLine(0, y2, x2, y2);
56
57         if (row + 1 != rowCount() || col + 1 != columnCount())
58                 return;
59
60         // draw handle
61         int const step = cellsize / 5;
62         int const space = 4;
63         int x = cellsize - step;
64         int const y = cellsize - space;
65         int const ex = cellsize - space;
66         int ey = cellsize - step;
67         while (x > space) {
68                 p->drawLine(x, y, ex, ey);
69                 x -= step;
70                 ey -= step;
71         }
72 }
73
74
75 void EmptyTable::setNumberColumns(int nr_cols)
76 {
77         if (nr_cols < 1)
78                 return;
79         if (nr_cols == columnCount())
80                 return;
81         setColumnCount(nr_cols);
82         resetCellSize();
83         updateGeometry();
84         // emit signal
85         colsChanged(nr_cols);
86 }
87
88
89 void EmptyTable::setNumberRows(int nr_rows)
90 {
91         if (nr_rows < 1)
92                 return;
93         if (nr_rows == rowCount())
94                 return;
95         setRowCount(nr_rows);
96         resetCellSize();
97         updateGeometry();
98         // emit signal
99         rowsChanged(nr_rows);
100 }
101
102 /*
103 void EmptyTable::mouseMoveEvent(QMouseEvent *ev)
104 {
105         int const x = ev->pos().x();
106         int const y = ev->pos().y();
107
108         if (x > 0)
109                 setNumberColumns(x / cellsize + columnCount()-1);
110
111         if (y > 0)
112                 setNumberRows(y / cellsize + rowCount()-1);
113 }
114 */
115
116 #include "EmptyTable_moc.cpp"
117
118
119 namespace lyx {
120
121
122 } // namespace lyx