]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QBrowseBox.C
the qtclean-2 patch with some very small changes
[lyx.git] / src / frontends / qt2 / QBrowseBox.C
1 /**
2  * \file QBrowseBox.C
3  *
4  * Original file taken from klyx 0.10 sources:
5  * $Id: QBrowseBox.C,v 1.6 2002/10/20 01:48:27 larsbj Exp $
6  *
7  * \author Kalle Dalheimer ?
8  *
9  * Full author contact details are available in file CREDITS
10  */
11
12 #include <config.h>
13
14 #include <qstring.h>
15 #include <qpixmap.h>
16 #include <qkeycode.h>
17 #include <qpainter.h>
18 #include <qapplication.h>
19 #include <qdrawutil.h>
20 #include <qstyle.h>
21 #include <qimage.h>
22
23 #include "QBrowseBox.h"
24
25 #include <cstdio>
26 #include <cmath>
27
28
29 QBrowseBox::QBrowseBox(int rows, int cols)
30         : QGridView()
31 {
32         setNumRows(rows);
33         setNumCols(cols);
34         setCellWidth(width()/cols);
35         setCellHeight(height()/rows);
36
37         texts_   = new QString[rows * cols];
38         pixmaps_ = new QPixmap[rows * cols];
39
40         activecell_.setX(0);
41         activecell_.setY(0);
42         updateCell(0, 0);
43         setMouseTracking(true);
44
45         if (style().inherits("QWindowsStyle"))
46                 setFrameStyle(QFrame::WinPanel | QFrame::Raised);
47         else
48                 setFrameStyle(QFrame::Panel | QFrame::Raised);
49
50         setFocusPolicy(QWidget::StrongFocus);
51 }
52
53
54
55 QBrowseBox::~QBrowseBox()
56 {
57         delete [] texts_;
58         delete [] pixmaps_;
59 }
60
61
62 int QBrowseBox::coordsToIndex(int row, int col)
63 {
64         if (col < 0 || col > numCols() || row < 0 ||  row > numRows())
65                 qDebug("coordsToIndex: invalid coords (%d, %d)\n", row, col);
66         return row + col * numCols();
67 }
68
69
70 void QBrowseBox::insertItem(QString const & text, int row, int col)
71 {
72         texts_[coordsToIndex(row, col)] = text;
73 }
74
75
76 void QBrowseBox::insertItem(char const * text, int x, int y)
77 {
78         insertItem(QString(text), x, y);
79 }
80
81
82 void QBrowseBox::insertItem(QPixmap pixmap, int row, int col)
83 {
84         pixmaps_[coordsToIndex(row, col)] = pixmap;
85 }
86
87
88 void QBrowseBox::insertItem( QPixmap pixmap)
89 {
90         int w = pixmap.width() / numCols();
91         int h = pixmap.height() / numRows();
92
93         for (int row = 0; row < numRows(); ++row)
94                 for (int col = 0; col < numCols(); ++col) {
95                         QPixmap small(w,h);
96                         bitBlt(&small,0,0,&pixmap,col*w,row*h,w,h,Qt::CopyROP,false);
97                         insertItem(small, row, col);
98                 }
99
100         resize(pixmap.width() + (numCols() + 1) * frameWidth(),
101                pixmap.height() + (numRows() + 1) * frameWidth());
102 }
103
104
105 void QBrowseBox::removeItem(int row, int col)
106 {
107         texts_[coordsToIndex(row, col)] = "";
108         pixmaps_[coordsToIndex(row, col)].resize(0, 0);
109 }
110
111
112 void QBrowseBox::clear()
113 {
114         for (int row = 0; row < numRows(); ++row)
115                 for (int col = 0; col < numCols(); ++col)
116                         removeItem(row, col);
117 }
118
119
120 QString QBrowseBox::text(int row, int col)
121 {
122         if (col < 0 || col >= numCols() || row < 0 || row >= numRows())
123                 return "";
124         return texts_[coordsToIndex(row, col)];
125 }
126
127
128 QPixmap QBrowseBox::pixmap(int row, int col)
129 {
130         if (col < 0 || col >= numCols() || row < 0 || row >= numRows())
131                 return QPixmap();
132         return pixmaps_[coordsToIndex(row, col)];
133 }
134
135
136 void QBrowseBox::keyPressEvent(QKeyEvent * e)
137 {
138         switch(e->key()) {
139         case Key_Up:
140                 moveUp();
141                 break;
142         case Key_Down:
143                 moveDown();
144                 break;
145         case Key_Left:
146                 moveLeft();
147                 break;
148         case Key_Right:
149                 moveRight();
150                 break;
151         case Key_Return:
152                 emit selected(activecell_.x(), activecell_.y());
153                 break;
154         default:
155                 e->ignore();
156         }
157 }
158
159
160 void QBrowseBox::mouseReleaseEvent(QMouseEvent *)
161 {
162         qWarning("mouse release");
163         emit selected( activecell_.x(), activecell_.y());
164 }
165
166
167 //void QBrowseBox::closeEvent(QCloseEvent * e)
168 //{
169 //      e->accept();
170 //      qApp->exit_loop();
171 //}
172
173 void QBrowseBox::paintCell(QPainter * painter, int row, int col)
174 {
175         painter->setClipRect(cellGeometry(row,col));//, QPainter::CoordPainter);
176         bool ispixmap = false;
177
178         if (!pixmaps_[coordsToIndex(row,col)].isNull()) {
179                 painter->drawPixmap(0,0,pixmaps_[coordsToIndex(row, col)]);
180                 ispixmap = true;
181         }
182
183         if (activecell_.x() == row && activecell_.y() == col) {
184                 if (ispixmap)
185                         qDrawShadeRect(painter, 0, 0, cellWidth(),
186                                        cellHeight(), colorGroup(), false, 1);
187                 else
188                         qDrawShadePanel(painter, 0, 0, cellWidth(),
189                                         cellHeight(), colorGroup(), false, 1);
190         } else {
191                 qDrawPlainRect(painter, 0, 0, cellWidth(),
192                                cellHeight(), colorGroup().background(), 1);
193         }
194
195         if (!texts_[coordsToIndex(row, col)].isEmpty()) {
196                 painter->drawText(0, 0, cellWidth(),
197                                   cellHeight(), AlignLeft,
198                                   texts_[coordsToIndex(row, col)]);
199         }
200         painter->setClipping(false);
201 }
202
203
204 void QBrowseBox::resizeEvent(QResizeEvent * e)
205 {
206         QGridView::resizeEvent(e);
207         setCellWidth(contentsRect().width() / numCols());
208         setCellHeight(contentsRect().height() / numRows());
209 }
210
211
212 void QBrowseBox::mouseMoveEvent(QMouseEvent * e)
213 {
214         qWarning("mouseMoveEvent");
215         int x = e->pos().x();
216         int y = e->pos().y();
217
218         int cellx;
219         int celly;
220
221         if (x < 0 || y < 0 || x > width() || y > height()) {
222                 // outside the box
223                 cellx = -1;
224                 celly = -1;
225         } else {
226 #ifdef WITH_WARNINGS
227 #warning Is the switch of x and y intended here? (Andre)
228 #endif
229                 celly = (int)floor( ((double)x) / ((double)cellWidth()) );
230                 cellx = (int)floor( ((double)y) / ((double)cellHeight()) );
231         }
232
233         if (activecell_.x() != cellx || activecell_.y() != celly) {
234                 qWarning("update");
235                 // mouse has been moved to another cell
236                 int oldactivecellx = activecell_.x();
237                 int oldactivecelly = activecell_.y();
238                 activecell_.setX(cellx);
239                 activecell_.setY(celly);
240                 // remove old highlighting
241                 updateCell(oldactivecellx, oldactivecelly);
242                 // set new highlighting
243                 updateCell(activecell_.x(), activecell_.y());
244         }
245 }
246
247
248 void QBrowseBox::moveLeft()
249 {
250         int const y = activecell_.y();
251
252         if (y>0)
253                 activecell_.setY(y - 1);
254
255         updateCell(activecell_.x(), y);
256         updateCell(activecell_.x(), activecell_.y());
257 }
258
259
260 void QBrowseBox::moveRight()
261 {
262         int const y = activecell_.y();
263
264         if (y < numCols() - 1)
265                 activecell_.setY(y+1);
266
267         updateCell(activecell_.x(), y);
268         updateCell(activecell_.x(), activecell_.y());
269 }
270
271
272 void QBrowseBox::moveUp()
273 {
274         int const x = activecell_.x();
275
276         if (x > 0)
277                 activecell_.setX(x - 1);
278
279         updateCell(x, activecell_.y());
280         updateCell(activecell_.x(), activecell_.y());
281 }
282
283
284 void QBrowseBox::moveDown()
285 {
286         int const x = activecell_.x();
287
288         if (x < numRows() - 1)
289                 activecell_.setX(x + 1);
290
291         updateCell(x, activecell_.y());
292         updateCell(activecell_.x(), activecell_.y());
293 }