]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/emptytable.C
Minipage is no more (long live the box inset)
[lyx.git] / src / frontends / qt2 / emptytable.C
1 /**
2  * \file emptytable.C
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
14 #include "emptytable.h"
15 #include <qpainter.h>
16
17 /**
18  * A simple widget for a quick "preview" in TabularCreateDialog
19  */
20
21 unsigned int const cellsize = 20;
22
23
24 EmptyTable::EmptyTable(QWidget * parent, const char * name)
25         : QtTableView(parent, name, WRepaintNoErase)
26 {
27         setNumCols(5);
28         setNumRows(5);
29         setCellWidth(cellsize);
30         setCellHeight(cellsize);
31         setTableFlags(Tbl_autoScrollBars);
32 }
33
34
35 QSize EmptyTable::sizeHint() const
36 {
37         return QSize(cellsize * numCols(), cellsize * numRows());
38 }
39
40
41 void EmptyTable::paintCell(QPainter * p, int row, int col)
42 {
43         int const x2 = cellWidth(col) - 1;
44         int const y2 = cellHeight(row) - 1;
45
46         p->fillRect(0, 0, x2, y2, QColor("white"));
47         p->drawLine(x2, 0, x2, y2);
48         p->drawLine(0, y2, x2, y2);
49
50         if (row + 1 != numRows() || col + 1 != numCols())
51                 return;
52
53         // draw handle
54         int const step = cellsize / 5;
55         int const space = 4;
56         int x = cellsize - step;
57         int const y = cellsize - space;
58         int const ex = cellsize - space;
59         int ey = cellsize - step;
60         while (x > space) {
61                 p->drawLine(x, y, ex, ey);
62                 x -= step;
63                 ey -= step;
64         }
65 }
66
67
68 void EmptyTable::setNumberColumns(int nr_cols)
69 {
70         if (nr_cols < 1)
71                 return;
72         if (nr_cols == numCols())
73                 return;
74         setAutoUpdate(false);
75         setNumCols(nr_cols);
76         updateGeometry();
77         setAutoUpdate(true);
78         update();
79         emit colsChanged(nr_cols);
80 }
81
82
83 void EmptyTable::setNumberRows(int nr_rows)
84 {
85         if (nr_rows < 1)
86                 return;
87         if (nr_rows == numRows())
88                 return;
89         setAutoUpdate(false);
90         setNumRows(nr_rows);
91         updateGeometry();
92         setAutoUpdate(true);
93         update();
94         emit rowsChanged(nr_rows);
95 }
96
97
98 void EmptyTable::mouseMoveEvent(QMouseEvent *ev)
99 {
100         int const x = ev->pos().x();
101         int const y = ev->pos().y();
102
103         if (x > 0)
104                 setNumberColumns(x / cellsize + leftCell());
105
106         if (y > 0)
107                 setNumberRows(y / cellsize + topCell());
108 }