forked from sjg20/paperman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
desktopmodel.cpp
2049 lines (1611 loc) · 50.1 KB
/
desktopmodel.cpp
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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
License: GPL-2
An electronic filing cabinet: scan, print, stack, arrange
Copyright (C) 2009 Simon Glass, chch-kiwi@users.sourceforge.net
.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
.
This program 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 General Public License for more details.
.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
X-Comment: On Debian GNU/Linux systems, the complete text of the GNU General
Public License can be found in the /usr/share/common-licenses/GPL file.
*/
#include <assert.h>
#include "config.h"
#include "qapplication.h"
#include "qcursor.h"
#include "qinputdialog.h"
#include "qlabel.h"
#include "qmessagebox.h"
//#include "q3popupmenu.h"
#include "qtimer.h"
#include <QDateTime>
#include <QDebug>
#include <QFileInfo>
#include <QIcon>
#include <QKeyEvent>
#include <QPixmap>
#include <QMouseEvent>
#include <QUndoStack>
#include "err.h"
#include "mem.h"
#include "desktopmodel.h"
#include "desktopundo.h"
#include "desk.h"
#include "file.h"
#include "maxview.h"
#include "op.h"
#include "paperstack.h"
#include "utils.h"
// time to delay between scanning each stack, should be 0 unless testing
#define DELAY_TIME 0 //1000
Desktopmodel::Desktopmodel(QObject *parent)
: QAbstractItemModel (parent)
{
// _desk = 0;
_updateTimer = new QTimer (this);
_updateTimer->setSingleShot (true);
_debug_level = 0;
_op = 0;
_subdirs = false;
_forceVisible = QString::null;
_drop_target = 0;
QFont font;
_fm = new QFontMetrics (font);
_scan_desk = 0;
_scan_file = 0;
_model_invalid = false;
_minor_change = false;
_need_scaled_image = false;
_cloned = false;
_flushTimer = new QTimer (this);
connect(_flushTimer, SIGNAL(timeout()), this, SLOT(flushAllDesks()));
_flushTimer->start(1000 * 60); // flush every minute
_undo = new Desktopundostack (this);
connect (_undo, SIGNAL (undoTextChanged (const QString &)),
this, SIGNAL (undoTextChanged (const QString &)));
connect (_undo, SIGNAL (redoTextChanged (const QString &)),
this, SIGNAL (redoTextChanged (const QString &)));
connect (_undo, SIGNAL (canUndoChanged (bool)),
this, SIGNAL (canUndoChanged (bool)));
connect (_undo, SIGNAL (canRedoChanged (bool)),
this, SIGNAL (canRedoChanged (bool)));
connect (_undo, SIGNAL (undoTextChanged (const QString &)),
this, SIGNAL (undoChanged ()));
connect (_undo, SIGNAL (redoTextChanged (const QString &)),
this, SIGNAL (undoChanged ()));
connect (_undo, SIGNAL (canUndoChanged (bool)),
this, SIGNAL (undoChanged ()));
connect (_undo, SIGNAL (canRedoChanged (bool)),
this, SIGNAL (undoChanged ()));
connect (_updateTimer, SIGNAL (timeout()), this, SLOT (nextUpdate ()));
connect (qApp, SIGNAL (aboutToQuit ()),
this, SLOT (aboutToQuit ()));
if (!_unknown)
{
_no_access = QPixmap (":images/images/no_access.xpm");
_unknown = QPixmap (":images/images/unknown.xpm");
Q_ASSERT (!_no_access.isNull ());
Q_ASSERT (!_unknown.isNull ());
}
}
Desktopmodel::~Desktopmodel()
{
delete _fm;
if (!_cloned)
while (!_desks.isEmpty ())
delete _desks.takeFirst ();
// ensure the maxdesk is correctly closed (writes the maxdesk.ini file)
// if (_desk)
// delete _desk;
}
#define MAX_ID 10240 // maximum ID we can assign to a 'desk' node
/** index points to a Desk */
#define IS_DESK(ind) ((unsigned)(ind).internalId () < MAX_ID)
/** index points to a File */
#define IS_FILE(ind) ((unsigned)(ind).internalId () >= MAX_ID)
#define DESK_INDEX(row) createIndex (row, 0, row + 10)
#define FILE_INDEX(row,f) createIndex (row, 0, (void *)f)
QVariant Desktopmodel::data(const QModelIndex &index, int role) const
{
// qDebug () << "data" << index << role;
if (!index.isValid() || _model_invalid)
return QVariant();
if (!IS_FILE (index))
return QVariant ();
Q_ASSERT (IS_FILE (index));
File *f = (File *)index.internalPointer ();
// qDebug () << f->filename () << role;
#ifdef CONFIG_delay_dirscan
// if we don't have valid information for this stack, make a note to get it
if (!f->valid () && role == Role_pixmap)
{
Desktopmodel *model = (Desktopmodel *)this;
// don't add if already there (can happen with multiple redraws of an item)
if (!model->_pending_scan_list.contains (index))
{
// restart the timer if not running
if (_pending_scan_list.isEmpty ())
_updateTimer->start (DELAY_TIME);
model->_pending_scan_list << index;
}
}
#endif // CONFIG_delay_dirscan
switch (role)
{
case Qt::DecorationRole :
return QIcon (f->pixmap ());
case Qt::DisplayRole :
case Qt::EditRole :
return f->basename ();
case Role_position :
return f->pos ();
case Role_preview_maxsize :
return f->previewMaxsize ();
case Role_pixmap :
return f->pixmap ();
case Role_pagenum :
return f->pagenum ();
case Role_pagename :
return f->pageTitle (-1);
case Role_pagecount :
return f->pagecount ();
case Role_title_maxsize :
return f->titleMaxsize ();
case Role_pagename_maxsize :
return f->pagenameMaxsize ();
// this is only a guess, since we don't know what font will be used
case Role_maxsize :
{
QSize size;
// use the maximum width
// for height, use the pixmap preview plus 3 lines of text
size = f->previewMaxsize ();
size = size.expandedTo (f->titleMaxsize ());
size = size.expandedTo (f->pagenameMaxsize ());
size.setHeight (f->previewMaxsize ().height ()
+ 3 * _fm->height ());
return size;
}
// returns a string list of all the page names
case Role_pagename_list :
{
QStringList name;
int i;
for (i = 0; i < f->pagecount (); i++)
name << f->pageTitle (i);
return name;
}
case Role_valid :
return f->valid ();
case Role_droptarget :
return _drop_target && index == *_drop_target;
case Role_message :
if (f->err ())
return f->err ()->errstr;
else
{
QString str;
str = imageInfo (index, f->pagenum (), true);
/*
util_bytes_to_user (numstr, f->size);
str = QString ("%1 page%2, %3").arg (f->pagecount).arg (f->pagecount > 1 ? "s" : "").arg (numstr);
*/
if (_subdirs)
str += ", at " + f->pathname ();
return str;
}
case Role_pathname :
return f->pathname ();
case Role_filename :
return f->filename ();
case Role_author :
return getAnnot (index, File::Annot_author);
case Role_title :
return getAnnot (index, File::Annot_title);
case Role_keywords :
return getAnnot (index, File::Annot_keywords);
case Role_notes :
return getAnnot (index, File::Annot_notes);
case Role_error :
if (f->err ())
return f->err ()->errstr;
break;
}
return QVariant();
}
bool Desktopmodel::setData(const QModelIndex &index, const QVariant &value, int role)
{
bool changed = false;
if (!index.isValid() || !IS_FILE (index))
return false;
Q_ASSERT (IS_FILE (index));
File *f = (File *)index.internalPointer ();
switch (role)
{
case Qt::EditRole :
renameStack (index, value.toString ());
break;
case Qt::DecorationRole :
case Qt::DisplayRole :
case Role_pixmap :
case Role_pagecount :
break;
case Role_pagename :
renamePage (index, value.toString ());
break;
case Role_preview_maxsize :
f->setPreviewMaxsize (value.toSize ());
break;
case Role_title_maxsize :
f->setTitleMaxsize (value.toSize ());
break;
case Role_pagename_maxsize :
f->setPagenameMaxsize (value.toSize ());
break;
case Role_position :
f->setPos (value.toPoint ());
changed = true;
break;
case Role_pagenum :
{
int newpage = value.toInt ();
if (newpage < 0)
newpage = 0;
if (newpage >= f->pagecount ())
newpage = f->pagecount () - 1;
if (newpage != f->pagenum ())
{
f->setPagenum (newpage);
changed = true;
}
break;
}
}
if (changed)
{
QString pagename;
f->pixmap (true);
//FIXME: better to emit our own signal which tells Desktopdelegate to just update the pixmap
_minor_change = true;
emit dataChanged (index, index);
_minor_change = false;
}
return true;
}
Qt::ItemFlags Desktopmodel::flags(const QModelIndex &index) const
{
if (index.isValid())
return (Qt::ItemIsEnabled | Qt::ItemIsSelectable
| Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | Qt::ItemIsEditable);
return Qt::ItemIsDropEnabled;
}
int Desktopmodel::rowCount(const QModelIndex &parent) const
{
int count;
if (!parent.isValid())
count = _desks.size ();
else if (IS_DESK (parent))
count = getDesk (parent)->rowCount (); //fileCount ();
else
count = 0;
// qDebug () << "rowCount" << parent << count;
return count;
}
int Desktopmodel::columnCount(const QModelIndex &parent) const
{
if (IS_DESK (parent))
return 1;
return 0;
}
Qt::DropActions Desktopmodel::supportedDropActions() const
{
return Qt::CopyAction | Qt::MoveAction;
}
QModelIndex Desktopmodel::index (int row, int column, const QModelIndex &parent) const
{
QModelIndex ind;
// debug () << "index row =" << row;
// if (row == -1)
// {
// debug () << "bad row =" << row;
// }
if (parent == QModelIndex ())
{
if (column == 0 && row >= 0 && row < _desks.size ())
// printf ("%d, pixmap = %p\n", row, di->pixmap);
ind = DESK_INDEX (row);
}
else if (IS_DESK (parent))
{
// the parent is a desk node
Desk *desk = _desks [parent.internalId () - 10];
if (column == 0 && row >= 0 && row < desk->fileCount ())
ind = FILE_INDEX (row, desk->getFile (row));
}
// qDebug () << "index" << parent << "row" << row << "ind" << ind;
return ind;
}
QModelIndex Desktopmodel::index (const QString &fname, QModelIndex parent) const
{
if (parent == QModelIndex ())
{
for (int row = 0; row < _desks.size (); row++)
{
Desk *desk = _desks [row];
if (desk->dir () == fname)
return DESK_INDEX (row);
}
}
else // must be a desk
{
Desk *desk = getDesk (parent);
int row;
File *f = desk->findFile (fname, row);
if (f)
return FILE_INDEX (row, f);
}
return QModelIndex ();
}
QModelIndex Desktopmodel::parent(const QModelIndex &item) const
{
if (IS_FILE (item))
{
// this is a file node, so the parent will be a desk
File *file = (File *)item.internalPointer ();
Desk *desk = file->desk ();
int row = _desks.indexOf (desk);
Q_ASSERT (row != -1);
// qDebug () << "parent" << item;
return DESK_INDEX (row);
}
// otherwise parent is the root index
return QModelIndex ();
}
bool Desktopmodel::removeDesk (const QString &pathname)
{
QModelIndex ind = index (pathname + "/", QModelIndex ());
if (ind == QModelIndex ())
{
qDebug () << "Cannot find desk for" << pathname;
return false;
}
// TODO: check that when re-adding it reuses the same Desk
if (!IS_DESK (ind))
{
qDebug() << "Cannot removeDesk() on a non-Desk";
return false;
}
Desk *desk = getDesk (ind);
Q_ASSERT (desk);
int item = ind.row ();
beginRemoveRows (ind.parent (), ind.row (), ind.row ());
delete desk;
_desks.removeAt(item);
endRemoveRows ();
return true;
}
void Desktopmodel::setModelConv (Desktopmodelconv *modelconv)
{
_modelconv = modelconv;
}
QDebug Desktopmodel::debug (void) const
{
return qDebug () << "Desktopmodel";
}
Desktopundostack *Desktopmodel::getUndoStack (void)
{
return _undo;
}
File *Desktopmodel::getFile (const QModelIndex &index) const
{
_modelconv->assertIsSource (0, &index, 0);
if (!index.isValid ())
return NULL;
if (!IS_FILE (index))
{
qDebug () << "bad getFile" << index << index.internalId ();
}
Q_ASSERT (IS_FILE (index));
File *f = (File *)index.internalPointer ();
// if (!f->max && di->valid)
// qDebug () << "max is null, di->valid=" << di->valid;
// Q_ASSERT (!di->valid || f->max);
return f;
}
Desk *Desktopmodel::getDesk (const QModelIndex &index) const
{
_modelconv->assertIsSource (0, &index, 0);
if (!index.isValid ())
return NULL;
Q_ASSERT (IS_DESK (index));
// debug () << "rowCount" << _items.size ();
Desk *desk = _desks [index.internalId () - 10];
// if (!f->max && di->valid)
// qDebug () << "max is null, di->valid=" << di->valid;
// Q_ASSERT (!di->valid || f->max);
return desk;
}
QString &Desktopmodel::deskRootDir (QModelIndex ind)
{
return _desks [ind.row ()]->rootDir ();
}
pagepos_info Desktopmodel::getPosData (const QModelIndex &index) const
{
pagepos_info pos;
pos.pos = QPoint (-1, -1);
pos.pagenum = -1;
if (index.isValid ())
{
File *f = getFile (index);
pos.pos = f->pos ();
pos.pagenum = f->pagenum ();
pos.pagecount = f->pagecount ();
}
return pos;
}
#if 0// these functions not needed?
void Desktopmodel::savePersistentIndexes (void)
{
QModelIndexList list = persistentIndexList ();
_persistent_filenames = listToFilenames (list);
qDebug () << "savePersistentIndexes" << _persistent_filenames.size ();
foreach (QString str, _persistent_filenames)
qDebug () << " " << str;
}
void Desktopmodel::restorePersistentIndexes (void)
{
qDebug () << "restorePersistentIndexes" << _persistent_filenames.size ();
foreach (QString str, _persistent_filenames)
qDebug () << " " << str;
QModelIndexList list = listFromFilenames (_persistent_filenames);
changePersistentIndexList (persistentIndexList (), list);
}
#endif
QString Desktopmodel::deskToDirname (QModelIndex parent)
{
Desk *desk = getDesk (parent);
return desk->dir ();
}
QModelIndex Desktopmodel::deskFromDirname (QString &dir)
{
return index (dir, QModelIndex ());
}
QStringList Desktopmodel::listToFilenames (const QModelIndexList &list)
{
QModelIndex ind;
QStringList slist;
// add the filename for each index to the list
foreach (ind, list)
if (ind.isValid ())
slist << getFile (ind)->filename ();
return slist;
}
QModelIndexList Desktopmodel::listFromFilenames (const QStringList &slist,
QModelIndex parent)
{
QString fname;
QModelIndex ind;
QModelIndexList list;
// work through each filename in turn
foreach (fname, slist)
{
// search through all files looking for a match
ind = index (fname, parent);
// did the file disappear since we added it to the undo list?
if (ind == QModelIndex ())
printf ("Warning: filename '%s' is not present, but should be\n", qPrintable (fname));
// add the model index (found or not) to the list
list << ind;
}
return list;
}
void Desktopmodel::sortForDelete (QModelIndexList &list)
{
if (list.size () < 2)
return;
// get a list of the rows
QList<int> ilist;
QModelIndex ind, parent = list [0].parent ();
foreach (ind, list)
ilist << ind.row ();
// sort the list
qSort (ilist.begin (), ilist.end (), qGreater<int> ());
// and create a sorted index list
list.clear ();
int i;
foreach (i, ilist)
{
list << index (i, 0, parent);
printf (" %d\n", i);
}
}
typedef struct sort_info
{
QModelIndex ind;
QPoint pos;
} sort_info;
static bool positionLessThan (const sort_info &s1, const sort_info &s2)
{
int diff;
diff = s2.pos.y () - s1.pos.y ();
if (diff > 80)
return true;
else if (diff < -80)
return false;
return s1.pos.x () < s2.pos.x ();
}
void Desktopmodel::sortByPosition (QModelIndexList &list)
{
if (list.size () < 2)
return;
// get a list of the positions
QList<sort_info> ilist;
QModelIndex ind, parent = list [0].parent ();
foreach (ind, list)
{
sort_info s;
s.ind = ind;
s.pos = getFile (ind)->pos ();
ilist << s;
}
// sort the list
qStableSort (ilist.begin (), ilist.end (), positionLessThan);
// and create a sorted index list
list.clear ();
sort_info s;
foreach (s, ilist)
list << index (s.ind.row (), 0, parent);
}
void Desktopmodel::buildItem (QModelIndex index)
{
// qDebug () << "buildItem" << index;
File *f = getFile (index);
// if we can't load it, still mark it as valid otherwise we will keep loading it
if (f->load ())
f->setValid (true);
/* if we are asking for a page that has not been added yet, do nothing.
This happens during scanning */
if (f->pagenum () < f->pagecount ())
f->pixmap (true); // regenerate the pixmap
/* tell the view that the data has changed. The view will request the
new data. Note that if we don't have information about the item
size or titlesize yet, this will be calculated by the view (actually
delegate) and then stored back in the model */
emit dataChanged (index, index);
}
void Desktopmodel::aboutToQuit (void)
{
// we need to make sure that the maxdesk.ini file is saved
/*FIXME: port this
if (_desk)
{
delete _desk;
_desk = 0;
}
*/
}
QString Desktopmodel::getAnnot (QModelIndex ind, File::e_annot type) const
{
File *f = getFile (ind);
QString text;
text.clear ();
if (f)
f->setErr (f->getAnnot (type, text));
return text;
}
/*************************** scanning support functions *****************************/
err_info *Desktopmodel::beginScan (QModelIndex parent, const QString &stack_name)
{
Desk *desk;
QModelIndexList list;
int row;
err_info *err = NULL;
// work out which desk to scan into
desk = getDesk (parent);
if (!desk)
err = err_make (ERRFN, ERR_do_not_have_a_valid_directory_to_scan_into);
qDebug () << "scan parent" << parent << desk;
if (!err)
// create the new stack
err = desk->addPaperstack (stack_name, _scan_file, row);
if (err)
{
// give up, and make a note to ignore future signals
_scan_err = true;
return err;
}
// stop this maxdesk from being disposed while we are scanning into it
desk->setAllowDispose (false);
// add to the model
qDebug () << "scan_file" << _scan_file;
newItem (row, parent, list);
emit beginningScan (list [0]);
/** set this last, since emitting beginningScan() may call checkScanStack()
which will get confused if we have already switch to the new stack */
_scan_desk = desk;
_scan_parent = parent;
_scan_err = false;
return NULL;
}
err_info *Desktopmodel::cancelScan (void)
{
if (_scan_err)
return NULL;
Q_ASSERT (_scan_desk && _scan_file);
QModelIndex ind = index (_scan_file->filename (), _scan_parent);
// emit a signal for the benefit of pagewidget - it will remove its pages
emit endingScan (true);
// allow the scanning maxdesk to be disposed (we have finished scanning into it)
_scan_desk->setAllowDispose (true);
QModelIndex parent = _scan_parent;
_scan_parent = QModelIndex ();
_scan_desk = 0;
_scan_file = 0;
// remove the stack in the maxdesk if it is there
return opDeleteStack (ind);
}
err_info *Desktopmodel::confirmScan (void)
{
// qDebug () << "Desktopmodel::confirmScan";
Q_ASSERT (_scan_desk && _scan_file);
QModelIndex ind = index (_scan_file->filename (), _scan_parent);
// flush the item
CALL (_scan_file->flush ());
// this comment might not be relevant since Desktopmodel was enhanced to have multiple maxdesks:
/* at this point, although we have just flushed the item, it is possible
that the model does not have a valid max pointer (getFile(ind)->max).
This can happen when the user starts a scan, then clicks to another
folder then clicks back. Since the scan has not been flushed to the
filesystem yet, its stack might be blank and will have given an error
when loaded.
We are about to commit the scan, which will rely on the max pointer
being correct. The fix for this is in scanCommitPages() where we
re-read the stack */
// emit a signal for the benefit of pagewidget
// this may call scanCommitPages()
emit endingScan (false);
_scan_desk = 0;
_scan_file = 0;
if (ind != QModelIndex ())
{
//desk->rescanFile (getFile (ind)); // shouldn't need to do this now
buildItem (ind);
}
return NULL;
}
err_info *Desktopmodel::addPageToScan (const Filepage *mp, const QString &coverageStr)
{
if (_scan_err)
return NULL;
CALL (_scan_file->addPage (mp, false));
// qDebug () << "Desktopmodel::addPageToScan, page count" << _scan_file->pagecount;
emit newScannedPage (coverageStr, mp->markBlank ());
return NULL;
}
void Desktopmodel::pageStarting (Paperscan &scan, const PPage *page)
{
if (_scan_err)
return;
// qDebug () << "pageStarting" << scan.getPagenum (page);
// no scaled image size registered as yet
_need_scaled_image = false;
_scaled_image_size = QSize ();
_scaled_linenum = 0;
emit beginningPage ();
}
void Desktopmodel::pageProgress (Paperscan &scan, const PPage *page)
{
QImage image;
const char *data = 0;
int scaled_linenum;
int size = 0;
bool ok;
if (_scan_err)
return;
ok = scan.getData (page, data, size);
// qDebug () << "pageProgress" << scan.getPagenum (page) << ok << size;
// emit dataAddedToPage (page, data, size);
if (ok && getNewScaledImage (scan, page, data, size, image, scaled_linenum))
{
// qDebug () << " newScaledImage";
emit newScaledImage (image, scaled_linenum);
}
}
#if QT_VERSION >= 0x040400
#define USE_24BPP
#endif
bool Desktopmodel::getNewScaledImage (Paperscan &scan, const PPage *page,
const char *data, int nbytes, QImage &image, int &scaled_linenum)
{
int width, height;
int depth, stride;
if (_need_scaled_image && scan.getPageDetails (page, width, height, depth, stride) && stride)
{
int linenum, lines;
// how many scan lines worth of data do we have?
lines = nbytes / stride;
// what scaled line number are we up to now?
linenum = lines * _scaled_image_size.height () / height;
// if no new lines, exit
// if (linenum == _scaled_linenum)
/** we must have at least 2 lines to work with to be sure of getting a
single line result */
if (linenum - _scaled_linenum < 2)
return false;
// start from the previous scaled line, to give us a a of margin
// otherwise we might get gaps in the final image
int scaled_from = _scaled_linenum;
if (scaled_from > 0)
scaled_from--;
/* we now need to generate an image from scaled lines _scaled_linenum
to linenum. First work out the input (unscaled) line numbers */
int from_linenum = scaled_from * height / _scaled_image_size.height ();
int to_linenum = linenum * height / _scaled_image_size.height ();
// qDebug () << "getNewScaledImage bytes " << nbytes << " lines from" << from_linenum << to_linenum;
Filepage::getImageFromLines (data + stride * from_linenum, width,
to_linenum - from_linenum, depth, stride, image);
if (image.format () == QImage::Format_Indexed8)
#ifdef USE_24BPP
image = image.convertToFormat (QImage::Format_RGB888);
#else
image = image.convertToFormat (QImage::Format_RGB32);
#endif
image = image.scaled (_scaled_image_size, Qt::KeepAspectRatio);
if (!image.height ())
return false;
// qDebug () << "desktopmodel image" << image.width () << image.height ()<< image.format ();
scaled_linenum = scaled_from;
_scaled_linenum = linenum;
return true;
}
return false;
}
void Desktopmodel::registerScaledImageSize (const QSize &size)
{
/* note that we need to generate scaled images (via emit newScaledImage()
whenever we receive new data for this page */
_need_scaled_image = true;
if (_scaled_image_size != size)
{
// if the size has changed, start the image again
_scaled_image_size = size;
_scaled_linenum = 0;
}
}
int Desktopmodel::getScanSize (void)
{