summaryrefslogtreecommitdiffstats
path: root/kviewshell/marklist.cpp
blob: a9c7518ad74e1afb8a74de6fda406dfc30a7a708 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
/* This file is part of the KDE project
   Copyright (C) 2004 Wilfried Huss <Wilfried.Huss@gmx.at>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include <config.h>

#include <tqcheckbox.h>
#include <tqimage.h>
#include <tqlabel.h>
#include <tqlayout.h>
#include <tqtooltip.h>
#include <tqlabel.h>
#include <tqwhatsthis.h>
#include <tqpainter.h>
#include <tqtimer.h>

#include <kapplication.h>
#include <kglobalsettings.h>
#include <klocale.h>
#include <kpopupmenu.h>
#include <kiconloader.h>
#include <kdebug.h>

#include "documentPageCache.h"
#include "kvsprefs.h"
#include "marklist.h"


#include "marklist.moc"


namespace {

/** Holds the icon used as a overlay on pages which are not drawn yet. */
TQPixmap* waitIcon = 0;

} // namespace anon


/****** ThumbnailWidget ******/

ThumbnailWidget::ThumbnailWidget(MarkListWidget* _parent, const PageNumber& _pageNumber, DocumentPageCache* _pageCache)
  : TQWidget(_parent), pageNumber(_pageNumber), pageCache(_pageCache), parent(_parent)
{
  setBackgroundMode(Qt::NoBackground);

  needsUpdating = true;

  if (!waitIcon)
  {
    waitIcon = new TQPixmap(KGlobal::iconLoader()->loadIcon("gear", KIcon::NoGroup, KIcon::SizeMedium));
  }
}

void ThumbnailWidget::paintEvent(TQPaintEvent* e)
{
  // Only repaint if the widget is really visible. We need to check this because Qt
  // sends paintEvents to all widgets that have ever been visible in the Scrollview
  // whenever the ScrollView is resized. This also increases the percieved performance
  // only thumbnails that are really needed are rendered.
  if (!parent->isVisible())
  {
    //kdDebug() << "Abort Thumbnail drawing for page " << pageNumber << endl;
    return;
  }

  TQPainter p(this);
  p.setClipRect(e->rect());

  // Paint a black border around the widget
  p.setRasterOp(Qt::CopyROP);
  p.setBrush(NoBrush);
  p.setPen(Qt::black);
  p.drawRect(rect());

  // Remove 1 pixel from all sides of the rectangle, to eliminate overdraw with
  // the black border.
  TQRect thumbRect = rect();
  thumbRect.addCoords(1,1,-1,-1);

  // If the thumbnail is empty or has been marked for updating generate a new thumbnail.
  if (thumbnail.isNull() || needsUpdating)
  {
    if (KVSPrefs::changeColors() && KVSPrefs::renderMode() == KVSPrefs::EnumRenderMode::Paper)
      p.fillRect(thumbRect, KVSPrefs::paperColor());
    else
      p.fillRect(thumbRect, Qt::white);

    // Draw busy indicator.
    // Im not really sure if this is a good idea.
    // While it is nice to see an indication that something is happening for pages which
    // take long to redraw, it gets quite annoing for fast redraws.
    // TODO: Disable or find something less distractiong.
    p.drawPixmap(10, 10, *waitIcon);

    TQTimer::singleShot(50, this, TQT_SLOT(setThumbnail()));
    return;
  }

  // Safety check
  if (thumbnail.isNull())
  {
    kdDebug(1223) << "No Thumbnail for page " << pageNumber << " created." << endl;
    return;
  }


  // The actual page starts at point (1,1) because of the outline.
  // Therefore we need to shift the destination rectangle.
  TQRect pixmapRect = thumbRect;
  pixmapRect.moveBy(-1,-1);

  // Paint widget
  bitBlt (this, thumbRect.topLeft(), &thumbnail, pixmapRect, CopyROP);
}

void ThumbnailWidget::resizeEvent(TQResizeEvent*)
{
  thumbnail.resize(width(), height());
  // Generate a new thumbnail in the next paintEvent.
  needsUpdating = true;
}

void ThumbnailWidget::setThumbnail()
{
  if (!parent->isVisible())
  {
    // We only want to calculate the thumbnail for widgets that are currently visible.
    // When we are fast scrolling thru the document. Many paint events are created, that
    // are often not needed anymore at the time the eventloop executes them.
    //kdDebug() << "Delayed request Abort Thumbnail drawing for page " << pageNumber << endl;
    kapp->processEvents();
    return;
  }

  needsUpdating = false;

  // Draw Thumbnail
  thumbnail = pageCache->createThumbnail(pageNumber, width() - 2);

  if (thumbnail.height() != height() + 2)
    setFixedHeight(thumbnail.height() + 2);

  update();
  kapp->processEvents();
}


/****** MarkListWidget ******/


MarkListWidget::MarkListWidget(TQWidget* _parent, MarkList* _markList, const PageNumber& _pageNumber,
                               DocumentPageCache* _pageCache, bool _showThumbnail)
  : TQWidget(_parent), showThumbnail(_showThumbnail), pageNumber(_pageNumber),
    pageCache(_pageCache), markList(_markList)
{
  TQBoxLayout* layout = new TQVBoxLayout(this, margin);

  thumbnailWidget = 0;
  if (showThumbnail)
  {
    thumbnailWidget = new ThumbnailWidget(this, pageNumber, pageCache);
    layout->addWidget(thumbnailWidget, 1, Qt::AlignTop);
  }

  TQBoxLayout* bottomLayout = new TQHBoxLayout(layout);

  checkBox = new TQCheckBox(TQString::null, this );
  checkBox->setFocusPolicy(TQWidget::NoFocus);
  TQToolTip::add(checkBox, i18n("Select for printing"));
  bottomLayout->addWidget(checkBox, 0, Qt::AlignAuto);

  pageLabel = new TQLabel(TQString("%1").arg(pageNumber), this);
  bottomLayout->addWidget(pageLabel, 1);

  _backgroundColor = KGlobalSettings::baseColor();

  // Alternate between colors.
  if ((pageNumber % 2 == 0) && KGlobalSettings::alternateBackgroundColor().isValid())
    _backgroundColor = KGlobalSettings::alternateBackgroundColor();

  setPaletteBackgroundColor( _backgroundColor );

  show();
}

bool MarkListWidget::isChecked() const
{
    return checkBox->isChecked();
}

void MarkListWidget::toggle()
{
    checkBox->toggle();
}

void MarkListWidget::setChecked( bool checked )
{
    checkBox->setChecked(checked);
}

void MarkListWidget::setSelected( bool selected )
{
    if (selected)
      setPaletteBackgroundColor( TQApplication::palette().active().highlight() );
    else
      setPaletteBackgroundColor( _backgroundColor );
}

int MarkListWidget::setNewWidth(int width)
{
  int height = QMAX(checkBox->height(), pageLabel->height()) + 2*margin;
  if (showThumbnail)
  {
    // Calculate size of Thumbnail
    int thumbnailWidth = QMIN(width, KVSPrefs::maxThumbnailWidth());
    int thumbnailHeight = (int)((thumbnailWidth - 2*margin - 2) / pageCache->sizeOfPage(pageNumber).aspectRatio() + 0.5) + 2;

    // Resize Thumbnail if necessary
    if (thumbnailWidget->size() != TQSize(thumbnailWidth, thumbnailHeight))
      thumbnailWidget->setFixedSize(thumbnailWidth - 2*margin, thumbnailHeight);

    height += thumbnailHeight + 2*margin;
  }

  setFixedSize(width, height);
  return height;
}

bool MarkListWidget::isVisible()
{
  TQRect visibleRect(markList->contentsX(), markList->contentsY(),
                   markList->visibleWidth(), markList->visibleHeight());
  TQRect widgetRect(markList->childX(this), markList->childY(this), width(), height());

  if (widgetRect.intersects(visibleRect))
    return true;

  return false;
}


void MarkListWidget::mousePressEvent(TQMouseEvent* e)
{
  // Select Page
  if (e->button() == LeftButton)
  {
    emit selected(pageNumber);
  }
  else if (e->button() == RightButton)
  {
    emit showPopupMenu(pageNumber, e->globalPos());
  }
}


/****** MarkList ******/


MarkList::MarkList(TQWidget* parent, const char* name)
  : TQScrollView(parent, name), clickedThumbnail(0), showThumbnails(true), contextMenu(0)
{
  currentPage = PageNumber::invalidPage;
  widgetList.setAutoDelete(true);
  setFocusPolicy( TQWidget::StrongFocus );
  //viewport()->setFocusPolicy( TQWidget::WheelFocus );
  setResizePolicy(TQScrollView::Manual);

  setVScrollBarMode(TQScrollView::AlwaysOn);
  setHScrollBarMode(TQScrollView::AlwaysOff);

  setSizePolicy(TQSizePolicy::MinimumExpanding, TQSizePolicy::MinimumExpanding);

  viewport()->setBackgroundMode(Qt::PaletteBase);
  enableClipper(true);
}

MarkList::~MarkList()
{
  delete contextMenu;
}

void MarkList::setPageCache(DocumentPageCache* _pageCache)
{
  pageCache = _pageCache;
}

TQValueList<int> MarkList::selectedPages() const
{
  TQValueList<int> list;
  MarkListWidget* item;
  for(unsigned int i = 0; i < widgetList.count(); i++)
  {
    item = widgetList[i];
    if (item->isChecked()) 
        list << (i + 1);
  }
  return list;
}

void MarkList::setNumberOfPages(int numberOfPages, bool _showThumbnails)
{
  showThumbnails = _showThumbnails;

  widgetList.resize(numberOfPages);

  int y = 0;

  for (int page = 1; page <= numberOfPages; page++)
  {
    MarkListWidget* item = new MarkListWidget(viewport(), this, page, pageCache, showThumbnails);

    connect(item, TQT_SIGNAL(selected(const PageNumber&)), this, TQT_SLOT(thumbnailSelected(const PageNumber&)));
    connect(item, TQT_SIGNAL(showPopupMenu(const PageNumber&, const TQPoint&)), this, TQT_SLOT(showPopupMenu(const PageNumber&, const TQPoint&)));

    widgetList.insert(page - 1, item);

    int height = item->setNewWidth(visibleWidth());
    addChild(item, 0, y);

    y += height;
  }
  resizeContents(visibleWidth(), y);
  viewport()->update();
}

void MarkList::thumbnailSelected(const PageNumber& pageNumber)
{
  // This variable is set to remember that the next call to setCurrentPageNumber
  // has been initiated with a left click on the thumbnail of page pageNumber.
  clickedThumbnail = pageNumber;
  emit selected(pageNumber);
}

void MarkList::setCurrentPageNumber(const PageNumber& pageNumber)
{
  if (!pageNumber.isValid() || pageNumber > (int)widgetList.count())
  {
    clickedThumbnail = 0;
    return;
  }

  if (currentPage == pageNumber)
    return;

  MarkListWidget* item;

  // Clear old selection
  if (currentPage.isValid() && currentPage <= (int)widgetList.count())
  {
    item = widgetList[currentPage - 1];
    item->setSelected(false);
  }

  // Draw new selection
  item = widgetList[pageNumber - 1];
  item->setSelected(true);

  // Make selected page visible if the current page has not been set with a mouseclick
  // in the thumbnail list. (We use this because it is a bit confusing if the element that
  // you have just clicked on, is scrolled away under the mouse cursor)
  if (clickedThumbnail != pageNumber)
    ensureVisible(childX(item), childY(item), 0, item->height());

  clickedThumbnail = 0;

  currentPage = pageNumber;
}

void MarkList::clear()
{
  currentPage = PageNumber::invalidPage;
  widgetList.resize(0);
}

void MarkList::selectAll()
{
  MarkListWidget* item;
  for (unsigned int i = 0; i < widgetList.count(); i++)
  {
    item = widgetList[i];
    item->setChecked(true);
  }
}

void MarkList::selectEven()
{
  MarkListWidget* item;
  for (unsigned int i = 1; i < widgetList.count(); i = i + 2)
  {
    item = widgetList[i];
    item->setChecked(true);
  }
}

void MarkList::selectOdd()
{
  MarkListWidget* item;
  for (unsigned int i = 0; i < widgetList.count(); i = i + 2)
  {
    item = widgetList[i];
    item->setChecked(true);
  }
}

void MarkList::toggleSelection()
{
  MarkListWidget* item;
  for (unsigned int i = 0; i < widgetList.count(); i++)
  {
    item = widgetList[i];
    item->toggle();
  }
}

void MarkList::removeSelection()
{
  MarkListWidget* item;
  for (unsigned int i = 0; i < widgetList.count(); i++)
  {
    item = widgetList[i];
    item->setChecked(false);
  }
}

void MarkList::viewportResizeEvent(TQResizeEvent*)
{
  MarkListWidget* item;

  int yold = contentsHeight();
  int y = 0;

  for (unsigned int i = 0; i < widgetList.count(); i++)
  {
    item = widgetList[i];
    int height = item->setNewWidth(visibleWidth());
    moveChild(item, 0, y);

    y += height;
  }
  resizeContents(visibleWidth(), y);

  // If the height of the content has changed
  if (yold != contentsHeight())
  {
    // Make sure the selected item is still visible.
    if (currentPage.isValid() && currentPage <= (int)widgetList.count())
    {
      item = widgetList[currentPage-1];
      ensureVisible(childX(item), childY(item), 0, item->height());
    }
  }

  viewport()->update();
}


void MarkList::updateWidgetSize(const PageNumber& pageNumber)
{
  // safety checks
  if (!pageNumber.isValid() || pageNumber > widgetList.count())
  {
    kdError() << "MarkList::updateWidgetSize called with invalid pageNumber " << pageNumber << endl;
    return;
  }

  MarkListWidget* item;

  // Resize the changed widget
  item = widgetList[pageNumber - 1];
  int height = item->setNewWidth(visibleWidth());
  int y = childY(item) + height;

  // Move the rest of the widgets
  for (unsigned int i = pageNumber; i < widgetList.count(); i++)
  {
    item = widgetList[i];
    int height = item->height();
    moveChild(item, 0, y);

    y += height;
  }
  resizeContents(contentsWidth(), y);

  viewport()->update();
}

void MarkList::mousePressEvent(TQMouseEvent* e)
{
  if (e->button() == RightButton)
  {
    // We call showPopupMenu with an invalid pageNumber to indicate that
    // the mouse does not point at a thumbnailWidget.
    showPopupMenu(PageNumber::invalidPage, e->globalPos());
  }
}

void MarkList::slotShowThumbnails(bool show)
{
  if (show != showThumbnails)
  {
    int numOfPages = widgetList.count();

    if (numOfPages == 0)
      return;

    // Save current page.
    PageNumber _currentPage = currentPage;

    // Save page selections.
    TQValueVector<bool> selections;
    selections.resize(widgetList.count());
    for (unsigned int i = 0; i < widgetList.count(); i++)
      selections[i] = widgetList[i]->isChecked();

    // Rebuild thumbnail widgets.
    clear();
    setNumberOfPages(numOfPages, show);

    // Restore current page.
    setCurrentPageNumber(_currentPage);

    // Restore page selections
    for (unsigned int i = 0; i < widgetList.count(); i++)
      widgetList[i]->setChecked(selections[i]);
  }
}


void MarkList::repaintThumbnails()
{
  bool show = showThumbnails;
  int numOfPages = widgetList.count();

  // Rebuild thumbnail widgets.
  clear();
  setNumberOfPages(numOfPages, show);
}


void MarkList::showPopupMenu(const PageNumber& pageNumber, const TQPoint& position)
{
  if (contextMenu == 0)
  {
    // Initialize Contextmenu
    contextMenu = new KPopupMenu(this, "markListContext");

    contextMenu->insertItem(i18n("Select &Current Page"), 0);
    contextMenu->insertItem(i18n("Select &All Pages"), 1);
    contextMenu->insertItem(i18n("Select &Even Pages"), 2);
    contextMenu->insertItem(i18n("Select &Odd Pages"), 3);
    contextMenu->insertItem(i18n("&Invert Selection"), 4);
    contextMenu->insertItem(i18n("&Deselect All Pages"), 5);
  }

  if (widgetList.count() == 0)
  {
    for (int i = 0; i <= 5; i++)
      contextMenu->setItemEnabled(i, false);
  }
  else
  {
    for (int i = 0; i <= 5; i++)
      contextMenu->setItemEnabled(i, true);
  }

  // Only allow to select the current page if we got a valid pageNumber.
  if (pageNumber.isValid() && pageNumber <= (int)widgetList.count())
    contextMenu->setItemEnabled(0, true);
  else
    contextMenu->setItemEnabled(0, false);

  // Show Contextmenu
  switch(contextMenu->exec(position))
  {
    case 0:
      widgetList[pageNumber - 1]->toggle();
      break;

    case 1:
      selectAll();
      break;

    case 2:
      selectEven();
      break;

    case 3:
      selectOdd();
      break;

    case 4:
      toggleSelection();
      break;

    case 5:
      removeSelection();
      break;
  }
}