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