Rev 23054 | Rev 23354 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
17513 | jghali | 1 | /* |
2 | For general Scribus (>=1.3.2) copyright and licensing information please refer |
||
3 | to the COPYING file provided with the program. Following this notice may exist |
||
4 | a copyright and/or license notice that predates the release of Scribus 1.3.2 |
||
5 | for which a new license (GPL+exception) is in place. |
||
6 | */ |
||
7 | |||
8 | #include <QApplication> |
||
9 | #include <QBitmap> |
||
10 | #include <QCursor> |
||
11 | #include <QDrag> |
||
12 | #include <QEvent> |
||
13 | #include <QHeaderView> |
||
14 | #include <QLabel> |
||
15 | #include <QList> |
||
16 | #include <QMenu> |
||
17 | #include <QMimeData> |
||
18 | #include <QMessageBox> |
||
19 | #include <QPainter> |
||
20 | |||
21 | #include "commonstrings.h" |
||
20185 | craig | 22 | #include "iconmanager.h" |
17513 | jghali | 23 | #include "pagelayout.h" |
24 | #include "pagepalette_widgets.h" |
||
25 | #include "sccombobox.h" |
||
26 | #include "scpage.h" |
||
19605 | jghali | 27 | #include "ui/scmessagebox.h" |
19080 | craig | 28 | |
19093 | craig | 29 | |
17513 | jghali | 30 | /* IconItems Code */ |
22603 | craig | 31 | SeItem::SeItem(const QString& text, uint nr, const QPixmap& Pix) |
32 | : QTableWidgetItem(QIcon(Pix), "", 1002) |
||
17513 | jghali | 33 | { |
34 | pageNumber = nr; |
||
35 | pageName = text; |
||
36 | setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled); |
||
37 | } |
||
38 | |||
39 | const QString& SeItem::getPageName() |
||
40 | { |
||
41 | return pageName; |
||
42 | } |
||
43 | |||
44 | /* ListBox Subclass */ |
||
45 | SeList::SeList(QWidget* parent) : QListWidget(parent) |
||
46 | { |
||
23199 | jghali | 47 | m_currItem = nullptr; |
48 | m_mousePressed = false; |
||
49 | m_thumb = false; |
||
17513 | jghali | 50 | setAcceptDrops(true); |
51 | } |
||
52 | |||
53 | void SeList::mouseReleaseEvent(QMouseEvent *m) |
||
54 | { |
||
23199 | jghali | 55 | m_mousePressed = false; |
17513 | jghali | 56 | if (m->button() == Qt::RightButton) |
57 | { |
||
58 | QMenu *pmen = new QMenu(); |
||
18181 | fschmid | 59 | // qApp->changeOverrideCursor(QCursor(Qt::ArrowCursor)); |
20424 | jghali | 60 | QAction *px = pmen->addAction( tr("Show Page Previews"), this, SLOT(toggleThumbnail())); |
17513 | jghali | 61 | px->setCheckable(true); |
23199 | jghali | 62 | if (m_thumb) |
17513 | jghali | 63 | px->setChecked(true); |
64 | pmen->exec(QCursor::pos()); |
||
65 | delete pmen; |
||
66 | } |
||
67 | QListWidget::mouseReleaseEvent(m); |
||
68 | } |
||
69 | |||
70 | void SeList::toggleThumbnail() |
||
71 | { |
||
23199 | jghali | 72 | m_thumb = !m_thumb; |
17513 | jghali | 73 | emit thumbnailChanged(); |
74 | } |
||
75 | |||
76 | void SeList::mousePressEvent(QMouseEvent* e) |
||
77 | { |
||
78 | e->accept(); |
||
23199 | jghali | 79 | m_currItem = nullptr; |
17513 | jghali | 80 | QListWidgetItem *i = itemAt(e->pos()); |
81 | if (i) |
||
82 | { |
||
23199 | jghali | 83 | m_currItem = i; |
84 | m_mousePos = e->pos(); |
||
85 | m_mousePressed = true; |
||
17513 | jghali | 86 | } |
87 | QListWidget::mousePressEvent(e); |
||
88 | } |
||
89 | |||
90 | void SeList::mouseMoveEvent(QMouseEvent* e) |
||
91 | { |
||
23199 | jghali | 92 | if ((m_mousePressed) && ((m_mousePos - e->pos()).manhattanLength() > 4)) |
17513 | jghali | 93 | { |
23199 | jghali | 94 | m_mousePressed = false; |
95 | QListWidgetItem *item = itemAt(m_mousePos); |
||
19618 | jghali | 96 | if (!item) |
97 | return; |
||
98 | QMimeData *mimeData = new QMimeData; |
||
99 | QString pageName = item->data(Qt::UserRole).toString(); |
||
100 | mimeData->setData("page/magic", "1" + pageName.toLocal8Bit()); |
||
101 | mimeData->setText("1" + pageName); |
||
102 | QDrag *dr = new QDrag(this); |
||
103 | dr->setMimeData(mimeData); |
||
23054 | craig | 104 | const QPixmap& pm = IconManager::instance().loadPixmap("doc.png"); |
19618 | jghali | 105 | dr->setPixmap(pm); |
106 | // dr->setDragCursor(pm, Qt::CopyAction); |
||
107 | // dr->setDragCursor(pm, Qt::MoveAction); |
||
108 | dr->exec(Qt::CopyAction | Qt::MoveAction); |
||
109 | QApplication::setOverrideCursor(Qt::ArrowCursor); |
||
17513 | jghali | 110 | } |
111 | } |
||
112 | |||
113 | void SeList::keyPressEvent(QKeyEvent * e) |
||
114 | { |
||
115 | bool accepted = false; |
||
116 | int k = e->key(); |
||
117 | if (k == Qt::Key_Delete) |
||
118 | { |
||
119 | if (currentItem()) |
||
120 | { |
||
121 | e->accept(); |
||
19605 | jghali | 122 | if (ScMessageBox::question(this, tr("Delete Master Page?"), |
17513 | jghali | 123 | "<qt>" + tr("Are you sure you want to delete this master page?") + "</qt>", |
19605 | jghali | 124 | QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes) == QMessageBox::Yes) |
17513 | jghali | 125 | { |
126 | emit DelMaster(currentItem()->text()); |
||
127 | } |
||
128 | accepted = true; |
||
129 | } |
||
130 | } |
||
131 | if (!accepted) |
||
132 | QListWidget::keyPressEvent(e); |
||
133 | } |
||
134 | |||
135 | |||
136 | /* QTable Subclass */ |
||
137 | SeView::SeView(QWidget* parent) : QTableWidget(parent) |
||
138 | { |
||
139 | setDragEnabled(true); |
||
140 | setAcceptDrops(true); |
||
20424 | jghali | 141 | setDropIndicatorShown(true); |
17513 | jghali | 142 | // viewport()->setAcceptDrops(true); |
143 | setShowGrid(false); |
||
144 | setWordWrap(true); |
||
23199 | jghali | 145 | m_mousePressed = false; |
146 | m_pageCount = 0; |
||
147 | m_colmult = 1; |
||
148 | m_coladd = 0; |
||
149 | m_rowmult = 2; |
||
150 | m_rowadd = 1; |
||
151 | m_cols = 1; |
||
152 | m_firstPage = 0; |
||
17513 | jghali | 153 | // setFocusPolicy(Qt::NoFocus); |
154 | } |
||
155 | |||
156 | void SeView::mousePressEvent(QMouseEvent* e) |
||
157 | { |
||
158 | e->accept(); |
||
23199 | jghali | 159 | m_mousePos = e->pos(); |
160 | m_mousePressed = true; |
||
17513 | jghali | 161 | QTableWidget::mousePressEvent(e); |
162 | } |
||
163 | |||
164 | void SeView::mouseReleaseEvent(QMouseEvent* e) |
||
165 | { |
||
166 | e->accept(); |
||
23199 | jghali | 167 | m_mousePressed = false; |
168 | |||
17513 | jghali | 169 | emit Click(rowAt(e->pos().y()), columnAt(e->pos().x()), e->button()); |
170 | QTableWidget::mouseReleaseEvent(e); |
||
171 | } |
||
172 | |||
173 | void SeView::mouseMoveEvent(QMouseEvent* e) |
||
174 | { |
||
23199 | jghali | 175 | if ((m_mousePressed) && ((m_mousePos - e->pos()).manhattanLength() > 4)) |
17513 | jghali | 176 | { |
23199 | jghali | 177 | m_mousePressed = false; |
17513 | jghali | 178 | int a = rowAt(e->pos().y()); |
179 | int b = columnAt(e->pos().x()); |
||
180 | if ((a != -1) && (b != -1)) |
||
181 | { |
||
182 | QTableWidgetItem* ite = item(a, b); |
||
22603 | craig | 183 | if (ite != nullptr) |
17513 | jghali | 184 | { |
185 | if (ite->type() == 1002) |
||
186 | { |
||
187 | SeItem* it = (SeItem*)ite; |
||
188 | QString str(it->pageName); |
||
189 | bool dummy; |
||
23199 | jghali | 190 | int p = getPage(a, b, &dummy); |
17513 | jghali | 191 | QString tmp; |
192 | QMimeData *mimeData = new QMimeData; |
||
23199 | jghali | 193 | mimeData->setData("page/magic", "2 " + tmp.setNum(p).toLocal8Bit() + " " + str.toLocal8Bit()); |
194 | mimeData->setText("2 " + tmp.setNum(p) + " " + str); |
||
17513 | jghali | 195 | QDrag *dr = new QDrag(this); |
196 | dr->setMimeData(mimeData); |
||
23054 | craig | 197 | const QPixmap& pm = IconManager::instance().loadPixmap("doc.png"); |
17513 | jghali | 198 | dr->setPixmap(pm); |
199 | // dr->setDragCursor(pm, Qt::CopyAction); |
||
200 | // dr->setDragCursor(pm, Qt::MoveAction); |
||
201 | dr->exec(Qt::CopyAction | Qt::MoveAction); |
||
202 | QApplication::setOverrideCursor(Qt::ArrowCursor); |
||
203 | } |
||
204 | } |
||
205 | } |
||
206 | } |
||
207 | QTableWidget::mouseMoveEvent(e); |
||
208 | } |
||
209 | |||
210 | void SeView::dropEvent(QDropEvent * e) |
||
211 | { |
||
212 | QString str, tmp; |
||
213 | bool lastPage = false; |
||
214 | if (e->mimeData()->hasFormat("page/magic")) |
||
215 | { |
||
20424 | jghali | 216 | e->setDropAction(Qt::MoveAction); |
17513 | jghali | 217 | e->accept(); |
218 | // HACK to prevent strange Qt4 cursor behaviour after dropping. It's examined by Trolltech now - PV. |
||
219 | // It's the one and only reason why to include QApplication here. |
||
220 | // But sadly this destroys our normal Cursors |
||
221 | // Fixed at least in Qt-4.4.2 |
||
222 | // QApplication::restoreOverrideCursor(); |
||
223 | str = e->mimeData()->text(); |
||
23199 | jghali | 224 | clearPix(); |
17513 | jghali | 225 | if (str.startsWith("1")) |
226 | { |
||
227 | int a = rowAt(e->pos().y()); |
||
228 | int b = columnAt(e->pos().x()); |
||
229 | int p; |
||
230 | tmp = str.remove(0,1); |
||
231 | if ((a == -1) || (b == -1)) |
||
232 | return; |
||
23199 | jghali | 233 | if (a == rowCount() - 1) |
17513 | jghali | 234 | { |
23199 | jghali | 235 | emit NewPage(m_pageCount, tmp); |
17513 | jghali | 236 | return; |
237 | } |
||
23199 | jghali | 238 | p = getPage(a, b, &lastPage); |
17513 | jghali | 239 | if (columnCount() == 1) |
240 | { |
||
241 | if ((a % 2) == 0) |
||
242 | emit NewPage(p, tmp); |
||
243 | else |
||
244 | { |
||
245 | emit UseTemp(tmp, p); |
||
246 | QTableWidgetItem* ite = item(a, b); |
||
22603 | craig | 247 | if (ite == nullptr) |
17513 | jghali | 248 | return; |
249 | if (ite->type() == 1002) |
||
250 | { |
||
251 | SeItem* it = (SeItem*)ite; |
||
252 | it->pageName = tmp; |
||
253 | } |
||
254 | } |
||
255 | return; |
||
256 | } |
||
22603 | craig | 257 | if ((b % 2) == 0) |
258 | { |
||
259 | if (lastPage) |
||
23199 | jghali | 260 | emit NewPage(p + 1, tmp); |
22603 | craig | 261 | else |
262 | emit NewPage(p, tmp); |
||
263 | } |
||
17513 | jghali | 264 | else |
265 | { |
||
22603 | craig | 266 | emit UseTemp(tmp, p); |
267 | QTableWidgetItem* ite = item(a, b); |
||
268 | if (ite == nullptr) |
||
269 | return; |
||
270 | if (ite->type() == 1002) |
||
17513 | jghali | 271 | { |
22603 | craig | 272 | SeItem* it = (SeItem*)ite; |
273 | it->pageName = tmp; |
||
17513 | jghali | 274 | } |
275 | } |
||
22603 | craig | 276 | return; |
17513 | jghali | 277 | } |
278 | if (str.startsWith("2")) |
||
279 | { |
||
280 | int st = str.indexOf(" "); |
||
23199 | jghali | 281 | int en = str.indexOf(" ", st + 1); |
17513 | jghali | 282 | tmp = str.mid(en+1); |
21952 | craig | 283 | int dr = str.midRef(st, en-st).toInt(); |
17513 | jghali | 284 | int a = rowAt(e->pos().y()); |
285 | int b = columnAt(e->pos().x()); |
||
286 | if ((a == -1) || (b == -1)) |
||
287 | return; |
||
288 | QTableWidgetItem* ite = item(a, b); |
||
23199 | jghali | 289 | int p = getPage(a, b, &lastPage); |
290 | if (a == rowCount() - 1) |
||
17513 | jghali | 291 | { |
292 | emit movePage(dr, p+1); |
||
293 | return; |
||
294 | } |
||
295 | if (columnCount() == 1) |
||
296 | { |
||
297 | if ((a % 2) == 0) |
||
298 | emit movePage(dr, p); |
||
299 | else |
||
300 | { |
||
301 | emit UseTemp(tmp, p); |
||
22603 | craig | 302 | if (ite == nullptr) |
17513 | jghali | 303 | return; |
304 | SeItem* it = (SeItem*)ite; |
||
305 | it->pageName = tmp; |
||
306 | } |
||
307 | return; |
||
308 | } |
||
22603 | craig | 309 | if ((b % 2) == 0) |
310 | emit movePage(dr, lastPage ? p+1 : p); |
||
17513 | jghali | 311 | else |
312 | { |
||
22603 | craig | 313 | emit UseTemp(tmp, p); |
314 | if (ite == nullptr) |
||
315 | return; |
||
316 | if (ite->type() == 1002) |
||
17513 | jghali | 317 | { |
22603 | craig | 318 | SeItem* it = (SeItem*)ite; |
319 | it->pageName = tmp; |
||
17513 | jghali | 320 | } |
321 | } |
||
22603 | craig | 322 | return; |
17513 | jghali | 323 | } |
324 | } |
||
325 | } |
||
326 | |||
327 | void SeView::dragEnterEvent(QDragEnterEvent *e) |
||
328 | { |
||
329 | if (e->mimeData()->hasFormat("page/magic")) |
||
330 | e->acceptProposedAction(); |
||
331 | } |
||
332 | |||
333 | void SeView::dragLeaveEvent(QDragLeaveEvent *) |
||
334 | { |
||
23199 | jghali | 335 | clearPix(); |
17513 | jghali | 336 | } |
337 | |||
338 | void SeView::dragMoveEvent(QDragMoveEvent *e) |
||
339 | { |
||
340 | if (e->mimeData()->hasFormat("page/magic")) |
||
341 | { |
||
342 | e->acceptProposedAction(); |
||
343 | int a = rowAt(e->pos().y()); |
||
344 | int b = columnAt(e->pos().x()); |
||
23199 | jghali | 345 | clearPix(); |
17513 | jghali | 346 | if ((a == -1) || (b == -1)) |
347 | return; |
||
348 | if (columnCount() == 1) |
||
349 | { |
||
350 | if ((a % 2) == 0) |
||
351 | { |
||
352 | item(a, 0)->setBackground(Qt::darkBlue); |
||
353 | } |
||
354 | } |
||
355 | else |
||
356 | { |
||
357 | if (((b % 2) == 0) || (a == rowCount()-1)) |
||
358 | { |
||
359 | item(a, b)->setBackground(Qt::darkBlue); |
||
360 | } |
||
361 | } |
||
362 | } |
||
363 | } |
||
364 | |||
365 | void SeView::keyPressEvent(QKeyEvent * e) |
||
366 | { |
||
367 | bool accepted = false; |
||
368 | int k = e->key(); |
||
369 | if (k == Qt::Key_Delete) |
||
370 | { |
||
371 | e->accept(); |
||
19605 | jghali | 372 | if (ScMessageBox::question(this, tr("Delete Page?"), |
17513 | jghali | 373 | "<qt>" + tr("Are you sure you want to delete this page?") + "</qt>", |
19605 | jghali | 374 | QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes) == QMessageBox::Yes) |
17513 | jghali | 375 | { |
376 | bool dummy; |
||
23199 | jghali | 377 | int pageToDelete = getPage(currentRow(), currentColumn(), &dummy); |
17513 | jghali | 378 | emit DelPage(pageToDelete); |
379 | } |
||
380 | accepted = true; |
||
381 | } |
||
382 | if (!accepted) |
||
383 | QTableWidget::keyPressEvent(e); |
||
384 | } |
||
385 | |||
23199 | jghali | 386 | void SeView::clearPix() |
17513 | jghali | 387 | { |
388 | int counter = 0; |
||
389 | int rowcounter = 0; |
||
390 | for (int a = 0; a < rowCount(); ++a) |
||
391 | { |
||
392 | counter = 0; |
||
393 | if (columnCount() == 1) |
||
394 | { |
||
395 | if ((a % 2) == 0) |
||
396 | { |
||
397 | item(rowcounter, 0)->setBackground(Qt::white); |
||
398 | rowcounter += 2; |
||
399 | } |
||
400 | } |
||
401 | else |
||
402 | { |
||
403 | for (int b = 0; b < columnCount(); ++b) |
||
404 | { |
||
405 | if ((b % 2) == 0) |
||
406 | { |
||
407 | item(rowcounter, counter)->setBackground(Qt::white); |
||
408 | counter += 2; |
||
409 | } |
||
410 | } |
||
411 | rowcounter++; |
||
412 | } |
||
413 | } |
||
414 | for (int c = 0; c < columnCount(); ++c) |
||
415 | { |
||
416 | item(rowCount()-1, c)->setBackground(Qt::white); |
||
417 | } |
||
418 | } |
||
419 | |||
23199 | jghali | 420 | int SeView::getPage(int r, int c, bool *last) |
17513 | jghali | 421 | { |
23199 | jghali | 422 | int counter = m_firstPage; |
17513 | jghali | 423 | int rowcounter = 0; |
23199 | jghali | 424 | int ret = m_pageCount - 1; |
17513 | jghali | 425 | *last = false; |
23199 | jghali | 426 | if (r == rowCount() - 1) |
17513 | jghali | 427 | { |
428 | *last = true; |
||
429 | return ret; |
||
430 | } |
||
23199 | jghali | 431 | if ((r == 0) && (c < m_firstPage * m_colmult + m_coladd)) |
17513 | jghali | 432 | return 0; |
23199 | jghali | 433 | for (int a = 0; a < m_pageCount; ++a) |
17513 | jghali | 434 | { |
23199 | jghali | 435 | if ((rowcounter * m_rowmult + m_rowadd == r) && (counter * m_colmult + m_coladd == c)) |
17513 | jghali | 436 | { |
437 | ret = a; |
||
438 | return ret; |
||
439 | } |
||
22603 | craig | 440 | if (columnCount() == 1) |
17513 | jghali | 441 | { |
23199 | jghali | 442 | if ((rowcounter * m_rowmult) == r) |
17513 | jghali | 443 | { |
22603 | craig | 444 | ret = a; |
445 | return ret; |
||
17513 | jghali | 446 | } |
22603 | craig | 447 | } |
448 | else |
||
449 | { |
||
23199 | jghali | 450 | if ((counter * m_colmult == c) && (rowcounter * m_rowmult + m_rowadd == r)) |
17513 | jghali | 451 | { |
22603 | craig | 452 | ret = a; |
453 | return ret; |
||
17513 | jghali | 454 | } |
455 | } |
||
456 | counter++; |
||
23199 | jghali | 457 | if (counter > m_cols - 1) |
17513 | jghali | 458 | { |
459 | counter = 0; |
||
460 | rowcounter++; |
||
461 | } |
||
462 | } |
||
463 | *last = true; |
||
464 | return ret; |
||
465 | } |
||
466 | |||
23199 | jghali | 467 | SeItem* SeView::getPageItem(int pageIndex) |
18558 | jghali | 468 | { |
469 | int rows = this->rowCount(); |
||
470 | int columns = this->columnCount(); |
||
471 | for (int i = 0; i < rows; ++i) |
||
472 | { |
||
473 | for (int j = 0; j < columns; ++j) |
||
474 | { |
||
475 | QTableWidgetItem* tbItem = item(i, j); |
||
476 | SeItem* pageItem = dynamic_cast<SeItem*>(tbItem); |
||
18559 | fschmid | 477 | if (pageItem && pageItem->pageNumber == static_cast<uint>(pageIndex)) |
18558 | jghali | 478 | return pageItem; |
479 | } |
||
480 | } |
||
22603 | craig | 481 | return nullptr; |
18558 | jghali | 482 | } |
483 | |||
17513 | jghali | 484 | /* Der Muelleimer */ |
485 | TrashBin::TrashBin(QWidget * parent) : QLabel(parent) |
||
486 | { |
||
23054 | craig | 487 | Normal = IconManager::instance().loadPixmap("trashcan.png"); |
488 | Offen = IconManager::instance().loadPixmap("trashcan2.png"); |
||
17513 | jghali | 489 | setPixmap(Normal); |
490 | setScaledContents(false); |
||
491 | setAcceptDrops(true); |
||
492 | } |
||
493 | |||
494 | void TrashBin::dragEnterEvent(QDragEnterEvent *e) |
||
495 | { |
||
496 | if (e->mimeData()->hasFormat("page/magic")) |
||
497 | { |
||
498 | e->accept(); |
||
499 | setPixmap(Offen); |
||
500 | } |
||
501 | } |
||
502 | |||
503 | void TrashBin::dragLeaveEvent(QDragLeaveEvent *) |
||
504 | { |
||
505 | setPixmap(Normal); |
||
506 | } |
||
507 | |||
508 | void TrashBin::dropEvent(QDropEvent * e) |
||
509 | { |
||
510 | setPixmap(Normal); |
||
511 | QString str, tmp; |
||
512 | if (e->mimeData()->hasFormat("page/magic")) |
||
513 | { |
||
514 | e->accept(); |
||
515 | str = e->mimeData()->text(); |
||
516 | if (str.startsWith("2")) |
||
517 | { |
||
518 | int st = str.indexOf(" "); |
||
519 | int en = str.indexOf(" ", st+1); |
||
21952 | craig | 520 | emit DelPage(str.midRef(st, en-st).toInt()); |
17513 | jghali | 521 | } |
522 | if (str.startsWith("1")) |
||
523 | { |
||
524 | tmp = str.remove(0,1); |
||
525 | emit DelMaster(tmp); |
||
526 | } |
||
527 | } |
||
528 | } |