]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/EmptyTable.cpp
Set a maximum value to zoom level
[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  * \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 unsigned int const cellheight = 20;
26 unsigned int const cellwidth = 30;
27
28
29 EmptyTable::EmptyTable(QWidget * parent, int rows, int columns)
30         : QTableWidget(rows, columns, parent)
31 {
32         resetCellSize();
33         setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
34         setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
35         viewport()->resize(cellheight * rows, cellwidth * columns);
36         setSelectionMode(QAbstractItemView::NoSelection);
37         setEditTriggers(QAbstractItemView::NoEditTriggers);
38 }
39
40
41 QSize EmptyTable::sizeHint() const
42 {
43         return QSize(cellwidth * (2 + columnCount()), cellheight * (2 + rowCount()));
44 }
45
46 void EmptyTable::resetCellSize()
47 {
48         for(int i = 0; i < rowCount(); ++i)
49                 setRowHeight(i, cellheight);
50         for(int i = 0; i < columnCount(); ++i)
51                 setColumnWidth(i, cellwidth);
52 }
53
54 void EmptyTable::paintCell(QPainter * p, int row, int col)
55 {
56         int const x2 = columnWidth(col) - 1;
57         int const y2 = rowHeight(row) - 1;
58
59         p->fillRect(0, 0, x2, y2, QColor("white"));
60         p->drawLine(x2, 0, x2, y2);
61         p->drawLine(0, y2, x2, y2);
62
63         if (row + 1 != rowCount() || col + 1 != columnCount())
64                 return;
65
66         // draw handle
67         int const step = cellheight / 5;
68         int const space = 4;
69         int x = cellwidth - step;
70         int const y = cellheight - space;
71         int const ex = cellwidth - space;
72         int ey = cellheight - step;
73         while (x > space) {
74                 p->drawLine(x, y, ex, ey);
75                 x -= step;
76                 ey -= step;
77         }
78 }
79
80
81 void EmptyTable::setNumberColumns(int nr_cols)
82 {
83         if (nr_cols < 1)
84                 return;
85         if (nr_cols == columnCount())
86                 return;
87         setColumnCount(nr_cols);
88         resetCellSize();
89         updateGeometry();
90         // emit signal
91         colsChanged(nr_cols);
92 }
93
94
95 void EmptyTable::setNumberRows(int nr_rows)
96 {
97         if (nr_rows < 1)
98                 return;
99         if (nr_rows == rowCount())
100                 return;
101         setRowCount(nr_rows);
102         resetCellSize();
103         updateGeometry();
104         // emit signal
105         rowsChanged(nr_rows);
106 }
107
108
109 void EmptyTable::mouseMoveEvent(QMouseEvent *ev)
110 {
111         int cc = columnCount();
112         int rc = rowCount();
113         int x = ev->x();
114         int y = ev->y();
115         int w = cellwidth * cc;
116         int h = cellheight * rc;
117         int wl = cellwidth * (cc - 1);
118         int hl = cellheight * (rc - 1);
119         if (x > w)
120                 setNumberColumns(cc + 1);
121         if (y > h)
122                 setNumberRows(rc + 1);
123         if (x < wl)
124                 setNumberColumns(cc - 1);
125         if (y < hl)
126                 setNumberRows(rc - 1);
127 }
128
129 #include "moc_EmptyTable.cpp"
130
131
132 namespace lyx {
133
134
135 } // namespace lyx