]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/emptytable.C
Lots and lots of little trivial bits.
[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 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include "emptytable.h"
18
19 /**
20  * A simple widget for a quick "preview" in TabularCreateDialog
21  */
22
23 const unsigned int cellsize = 20;
24
25 EmptyTable::EmptyTable(QWidget * parent, const char * name)
26         : QtTableView(parent, name, WRepaintNoErase)
27 {
28         setNumCols(5);
29         setNumRows(5);
30         setCellWidth(cellsize);
31         setCellHeight(cellsize);
32         setTableFlags(Tbl_autoScrollBars);
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 void EmptyTable::setNumberColumns(int nr_cols)
68 {
69         if (nr_cols < 1)
70                 return;
71         if (nr_cols == numCols())
72                 return;
73         setAutoUpdate(false);
74         setNumCols(nr_cols);
75         updateGeometry();
76         setAutoUpdate(true);
77         update();
78         emit colsChanged(nr_cols);
79 }
80
81 void EmptyTable::setNumberRows(int nr_rows)
82 {
83         if (nr_rows < 1)
84                 return;
85         if (nr_rows == numRows())
86                 return;
87         setAutoUpdate(false);
88         setNumRows(nr_rows);
89         updateGeometry();
90         setAutoUpdate(true);
91         update();
92         emit rowsChanged(nr_rows);
93 }
94
95 void EmptyTable::mouseMoveEvent(QMouseEvent *ev)
96 {
97         int const x = ev->pos().x();
98         int const y = ev->pos().y();
99
100         if (x > 0)
101                 setNumberColumns(x / cellsize + leftCell());
102
103         if (y > 0)
104                 setNumberRows(y / cellsize + topCell());
105 }