-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathdesktopwidget.cpp
1604 lines (1269 loc) · 45.4 KB
/
desktopwidget.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, [email protected]
.
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 <QtGui>
#include <QCheckBox>
#include <QFileDialog>
#include <QKeyEventTransition>
#include <QMenu>
#include <QMessageBox>
#include <QSettings>
#include <QItemSelectionModel>
#include <QToolBar>
#include <QToolButton>
#include "qapplication.h"
#include "qcursor.h"
#include "qdir.h"
#include "qfileinfo.h"
#include "qpointer.h"
#include "qinputdialog.h"
#include "qevent.h"
#include "qsplitter.h"
#include "qtimer.h"
#include <QPixmap>
#include "err.h"
#include "desktopdelegate.h"
#include "desktopmodel.h"
#include "desktopview.h"
#include "desktopundo.h"
#include "desktopwidget.h"
#include "dirmodel.h"
#include "dirview.h"
#include "folderlist.h"
#include "op.h"
#include "desk.h"
#include "mainwindow.h"
#include "maxview.h"
#include "pagewidget.h"
#include "senddialog.h"
#include "utils.h"
#include "ui_search.h"
Desktopwidget::Desktopwidget (QWidget *parent)
: QSplitter (parent)
{
_act_duplicate = nullptr;
_showing_imports = false;
_model = new Dirmodel ();
// _model->setLazyChildCount (true);
_dir = new Dirview (this);
_dir_proxy = new Dirproxy();
_dir_proxy->setSourceModel(_model);
_dir->setModel(_dir_proxy);
_contents = new Desktopmodel (this);
_toolbar = new Toolbar();
connect(_toolbar->pPrev, SIGNAL(clicked()), this, SLOT(pageLeft()));
connect(_toolbar->pNext, SIGNAL(clicked()), this, SLOT(pageRight()));
connect(_toolbar->prev, SIGNAL(clicked()), this, SLOT(stackLeft()));
connect(_toolbar->next, SIGNAL(clicked()), this, SLOT(stackRight()));
connect(_toolbar->pressed_esc, SIGNAL(triggered()),
this, SLOT(resetFilter()));
connect(_toolbar->cancelFilter, SIGNAL(clicked()),
this, SLOT(resetFilter()));
connect(_toolbar->match, SIGNAL(textChanged(const QString&)),
this, SLOT(matchChange(const QString &)));
QWidget *group = new QWidget(this);
_view = new Desktopview (group);
QVBoxLayout *lay = new QVBoxLayout (group);
lay->setContentsMargins (0, 0, 0, 0);
lay->setSpacing (2);
lay->addWidget (_toolbar);
lay->addWidget (_view);
connect (_view, SIGNAL (itemPreview (const QModelIndex &, int, bool)),
this, SLOT (slotItemPreview (const QModelIndex &, int, bool)));
connect(_view, SIGNAL(escapePressed()), this, SLOT(exitSearch()));
connect(_view, SIGNAL(newSelection()), this, SLOT(updateActions()));
_contents_proxy = new Desktopproxy (this);
_contents_proxy->setSourceModel (_contents);
_view->setModel (_contents_proxy);
// printf ("contents=%p, proxy=%p\n", _contents, _proxy);
// set up the model converter
_modelconv = new Desktopmodelconv (_contents, _contents_proxy);
// setup another one for Desktopmodel, which only allows assertions
_modelconv_assert = new Desktopmodelconv (_contents, _contents_proxy, false);
_view->setModelConv (_modelconv);
_contents->setModelConv (_modelconv_assert);
_delegate = new Desktopdelegate (_modelconv, this);
_view->setItemDelegate (_delegate);
connect (_delegate, SIGNAL (itemClicked (const QModelIndex &, int)),
this, SLOT (slotItemClicked (const QModelIndex &, int)));
connect (_delegate, SIGNAL (itemPreview (const QModelIndex &, int, bool)),
this, SLOT (slotItemPreview (const QModelIndex &, int, bool)));
connect (_delegate, SIGNAL (itemDoubleClicked (const QModelIndex &)),
this, SLOT (openStack (const QModelIndex &)));
connect (_contents, SIGNAL (undoChanged ()),
this, SIGNAL (undoChanged ()));
connect (_contents, SIGNAL (dirChanged (QString&, QModelIndex&)),
this, SLOT (slotDirChanged (QString&, QModelIndex&)));
connect (_contents, SIGNAL (beginningScan (const QModelIndex &)),
this, SLOT (slotBeginningScan (const QModelIndex &)));
connect (_contents, SIGNAL (endingScan (bool)),
this, SLOT (slotEndingScan (bool)));
connect (_contents, SIGNAL(updateRepositoryList (QString &, bool)),
this, SLOT(slotUpdateRepositoryList (QString &, bool)));
// position the items when the model is reset, otherwise things
// move and look ugly for a while
connect (_contents, SIGNAL (modelReset ()), _view, SLOT (setPositions ()));
createPage();
// and when there are no selected items
connect (_view, SIGNAL (pageLost()), _page, SLOT (slotReset ()));
_main = static_cast<Mainwidget *>(parent);
// setup the preview timer
_timer = new QTimer ();
_timer->setSingleShot (true);
connect (_timer, SIGNAL(timeout()), this, SLOT(updatePreview()));
connect (_dir, SIGNAL (clicked (const QModelIndex&)),
this, SLOT (dirSelected (const QModelIndex&)));
connect (_dir, SIGNAL (activated (const QModelIndex&)),
this, SLOT (dirSelected (const QModelIndex&)));
connect (_model, SIGNAL(droppedOnFolder(const QMimeData *, QString &)),
this, SLOT(slotDroppedOnFolder(const QMimeData *, QString &)));
/* notice when the current directory is fully displayed so we can handle
any pending action */
connect (_contents, SIGNAL (updateDone()), this, SLOT (slotUpdateDone()));
// connect signals from the directory tree
connect (_dir->_search, SIGNAL(triggered()), this, SLOT(searchInFolders()));
connect (_dir->_new, SIGNAL (triggered ()), this, SLOT (newDir ()));
connect (_dir->_rename, SIGNAL (triggered ()), this, SLOT (renameDir ()));
connect (_dir->_delete, SIGNAL (triggered ()), this, SLOT (deleteDir ()));
connect (_dir->_refresh, SIGNAL (triggered ()), this, SLOT (refreshDir ()));
connect (_dir->_add_recent, SIGNAL (triggered ()), this,
SLOT (addToRecent ()));
connect (_dir->_add_repository, SIGNAL (triggered ()), this,
SLOT (slotAddRepository ()));
connect (_dir->_remove_repository, SIGNAL (triggered ()), this,
SLOT (slotRemoveRepository ()));
connect(_dir->_refresh_cache, SIGNAL(triggered ()), this,
SLOT(slotRefreshCache()));
connect(_toolbar->exitSearch, SIGNAL(clicked()), this, SLOT(exitSearch()));
setStretchFactor(indexOf(_dir), 0);
QList<int> size;
if (!getSettingsSizes ("desktopwidget/", size))
{
size.append (200);
size.append (1000);
size.append (400);
}
setSizes (size);
connect (_view, SIGNAL (popupMenu (QModelIndex &)),
this, SLOT (slotPopupMenu (QModelIndex &)));
// allow top level to see our view messages
connect (_view, SIGNAL (newContents (QString)), this, SIGNAL (newContents (QString)));
addActions();
/* unfortunately when we first run maxview it starts with the main window
un-maximised. This means that scrollToLast() doesn't quite scroll far
enough for the maximised view which appears soon afterwards. As a hack
for the moment, we do another scroll 1 second after starting up */
QTimer::singleShot(1000, _view, SLOT (scrollToLast()));
}
void Desktopwidget::createPage(void)
{
_page = new Pagewidget (_modelconv, "desktopwidget/", this);
_page->setSmoothing (false);
// allow top level to see our preview messages
connect (_page, SIGNAL (newContents (QString)), this, SIGNAL (newContents (QString)));
connect (_page, SIGNAL (modeChanging (int, int)),
this, SLOT (slotModeChanging (int, int)));
_page->init ();
// alert the page widget whenever a new page is finished scanning
connect (_contents, SIGNAL (newScannedPage (const QString &, bool)),
_page, SLOT (slotNewScannedPage (const QString &, bool)));
// alert the page widget whenever we start to scan a new page
connect (_contents, SIGNAL (beginningPage ()),
_page, SLOT (slotBeginningPage ()));
// and when we have a new preview image fragment for the page being scanned
connect (_contents, SIGNAL (newScaledImage (const QImage &, int)),
_page, SLOT (slotNewScaledImage (const QImage &, int)));
// and when we change a stack
connect (_contents, SIGNAL (dataChanged (const QModelIndex &, const QModelIndex &)),
_page, SLOT (slotStackChanged (const QModelIndex &, const QModelIndex &)));
// and when we delete any stacks
connect (_contents, SIGNAL (rowsRemoved (const QModelIndex &, int, int)),
_page, SLOT (slotReset ()));
// and when we want to commit the stack
connect (_contents, SIGNAL (commitScanStack ()),
_page, SLOT (slotCommitScanStack ()));
}
void Desktopwidget::addActions(void)
{
// use translatable version of keys
addAction (_act_duplicate, "&Duplicate", SLOT(duplicate ()), "Ctrl+D");
addAction (_act_locate, "&Locate folder", SLOT(locateFolder ()), "Ctrl+Shift+L");
addAction (_act_delete, "D&elete stack", SLOT(deleteStacks ()), "Delete");
addAction (_act_stack, "&Stack", SLOT(stackPages()), "Ctrl+G");
addAction (_act_unstack_page, "Unstack &page", SLOT(unstackPage()), "Ctrl+I");
addAction (_act_unstack_all, "&Unstack all", SLOT(unstackStacks ()), "Ctrl+U");
addAction (_act_rename_stack, "&Rename stack", SLOT(renameStack ()), "F2"); //"F2,Ctrl+R");
addAction (_act_rename_page, "Re&name page", SLOT (renamePage ()), "Shift+F2");
addAction (_act_duplicate_page, "Duplicate p&age", SLOT (duplicatePage ()), "Ctrl+Shift+I");
addAction (_act_duplicate_max, "as &Max", SLOT (duplicateMax ()), "Ctrl+Shift+D");
addAction (_act_duplicate_pdf, "as &PDF", SLOT (duplicatePdf ()), "Ctrl+Shift+P");
// addAction (_act_duplicate_tiff, "as &Tiff", SLOT (duplicateTiff ()), "Ctrl+Shift+T");
addAction (_act_duplicate_odd, "&odd pages only", SLOT (duplicateOdd ()), "");
addAction (_act_duplicate_even, "&even pages only", SLOT (duplicateEven ()), "");
addAction (_act_duplicate_jpeg, "as &JPEG", SLOT (duplicateJpeg ()), "Ctrl+Shift+J");
addAction (_act_email, "&Files", SLOT (email ()), "Ctrl+E");
addAction (_act_email_pdf, "as &PDF", SLOT (emailPdf ()), "Ctrl+Shift+E");
addAction (_act_email_max, "as &Max", SLOT (emailMax ()), "Ctrl+Alt+E");
addAction (_act_move, "&Move to folder", SLOT(moveToFolder ()), "Ctrl+M");
// addAction (_act_send, "&Send stacks", SLOT (send ()), "Ctrl+S");
// addAction (_act_deliver_out, "&Delivery outgoing", SLOT (deliverOut ()), "");
updateActions();
}
Desktopwidget::~Desktopwidget ()
{
delete _timer;
delete _contents;
delete _modelconv;
delete _modelconv_assert;
delete _dir;
}
void Desktopwidget::closing (void)
{
QList<int> size = sizes ();
setSettingsSizes ("desktopwidget/", size);
_page->closing ();
}
QList<err_info> Desktopwidget::addRepositories(const QStringList& dirs)
{
QList<err_info> err_list;
QSettings qs;
err_info *err;
int size = qs.beginReadArray ("repository");
for (int i = 0; i < size; i++) {
qs.setArrayIndex (i);
err = addDir (qs.value ("path").toString (), true);
if (err)
err_list << *err;
}
qs.endArray ();
/* Add dirs for any arguments */
foreach (auto dirName, dirs) {
err = addDir(dirName);
if (err)
err_list << *err;
}
return err_list;
}
void Desktopwidget::slotModeChanging (int new_mode, int old_mode)
{
// qDebug () << "slotModeChanging" << new_mode << old_mode;
// get the current sizes and save them
if (old_mode != Pagewidget::Mode_none)
{
QList<int> size = sizes ();
QString str = QString ("desktopwidget/mode%2/").arg (old_mode);
setSettingsSizes (str, size);
}
if (new_mode != Pagewidget::Mode_none)
{
QList<int> size;
QString str = QString ("desktopwidget/mode%2/").arg (new_mode);
if (getSettingsSizes (str, size))
setSizes (size);
}
}
/***************************** scanning *********************************/
void Desktopwidget::slotBeginningScan (const QModelIndex &sind)
{
// qDebug () << "slotBeginningScan";
_modelconv->assertIsSource (0, &sind, 0);
// convert to a proxy index, since that is what _view uses
QModelIndex ind = sind;
_modelconv->indexToProxy (ind.model (), ind);
// scroll so the new stack is visible
_view->scrollTo (ind);
// select this new stack
_view->setSelectionRange (ind.row (), 1);
// advise the page widget that we are starting a scan
_page->beginningScan (ind);
}
void Desktopwidget::slotEndingScan (bool cancel)
{
// qDebug () << "slotEndingScan";
_page->endingScan (cancel);
}
void Desktopwidget::scanComplete (void)
{
_page->scanComplete ();
}
/***************************************************************************/
void Desktopwidget::addAction (QAction *&act, const char *text, const char *slot, const QString &shortcut,
QWidget *parent, const char *image)
{
if (!parent)
parent = _view;
act = new QAction (tr (text), parent);
if (!shortcut.isEmpty ())
act->setShortcut (tr (shortcut.toLatin1()));
if (image)
{
QIcon icon;
QString str = QString (":/images/images/%1").arg (image);
icon.addPixmap (QPixmap(str), QIcon::Normal, QIcon::Off);
act->setIcon (icon);
// act->setIconSize(QSize(24, 24));
act->setAutoRepeat (true);
}
parent->addAction (act);
connect (act, SIGNAL (triggered()), this, slot);
}
bool Desktopwidget::getCurrentFile (QModelIndex &index)
{
index = _view->getSelectedItem ();
return index.isValid ();
}
err_info *Desktopwidget::addDir (QString in_dirname, bool ignore_error)
{
err_info *err = NULL;
QDir dir (in_dirname);
// dirname = dir.absPath () + "/";
QString dirname = dir.canonicalPath ();
if (dirname.isEmpty ())
{
dirname = in_dirname;
if (dirname.endsWith ("/"))
dirname.chop (1);
err = err_make (ERRFN, ERR_directory_not_found1,
qPrintable(dirname));
}
// Check that the dirname isn't overlapping another
CALL (_model->checkOverlap (dirname, in_dirname));
dirname += "/";
QModelIndex index = _model->index (dirname, 0);
if (index != QModelIndex ())
return err_make (ERRFN, ERR_directory_is_already_present_as2,
qPrintable(in_dirname), qPrintable(dirname));
else if (err && !ignore_error)
;
else if (_model->addDir (dirname, ignore_error))
{
QModelIndex src_ind = _model->index(dirname);
index = _dir_proxy->mapFromSource(src_ind);
selectDir(index);
}
else
err = err_make (ERRFN, ERR_directory_could_not_be_added1,
qPrintable(in_dirname));
return err;
}
void Desktopwidget::selectDir(const QModelIndex &target, bool forceChange)
{
// int count = _model->rowCount (QModelIndex ());
/* use the second directory if there is nothing supplied, since the first
is 'Recent items' */
QModelIndex ind = target;
if (ind == QModelIndex())
ind = _dir_proxy->index(1, 0, QModelIndex());
//QModelIndex src_ind = _dir_proxy->mapToSource(ind);
//qDebug () << "Desktopwidget::selectDir" << _model->data(src_ind, Dirmodel::FilePathRole).toString ();
_dir->setCurrentIndex(ind);
_dir->setExpanded(ind, true);
dirSelected(ind, false, forceChange);
}
void Desktopwidget::slotDroppedOnFolder(const QMimeData *data, QString &dir)
{
QByteArray encodedData = data->data("application/vnd.text.list");
QDataStream stream(&encodedData, QIODevice::ReadOnly);
QStringList newItems;
while (!stream.atEnd()) {
QString text;
stream >> text;
newItems << text;
}
QModelIndexList list = _contents->listFromFilenames (newItems, _view->rootIndexSource ());
dir += "/";
QStringList sl;
_contents->moveToDir (list, _view->rootIndexSource (), dir, sl);
// event->acceptAction ();
}
void Desktopwidget::renameDir ()
{
QString path = _dir->menuGetPath ();
QString fullPath;
bool ok;
QString oldName = _dir->menuGetName ();
QModelIndex index;
index = _dir->menuGetModelIndex ();
QModelIndex src_ind = _dir_proxy->mapToSource(index);
if (_model->findIndex(src_ind) != -1)
{
QMessageBox::warning (0, "Maxview", "You cannot rename a root directory");
return;
}
QString text = QInputDialog::getText(
this, "Maxview", "Enter new directory name:", QLineEdit::Normal,
oldName, &ok);
if ( ok && !text.isEmpty() && text != oldName)
{
QDir dir;
QModelIndex src_parent = _model->parent(src_ind);
// if (!_model->setData (index, QVariant (text)))
path.truncate (path.length () - oldName.length () - 1);
fullPath = path + "/" + text;
if (dir.rename (path + "/" + oldName, fullPath))
_model->refresh(src_parent);
// _dir->refreshItemRename (text); // indicates current item has new children
else
QMessageBox::warning (0, "Maxview", "Could not rename directory");
}
}
void Desktopwidget::refreshDir()
{
QModelIndex index = _dir->menuGetModelIndex ();
QModelIndex src_ind = _dir_proxy->mapToSource(index);
// update the model with this new directory
_model->refresh(src_ind);
/* Now refresh the Desktopview */
QModelIndex sind = _contents->refresh(_path);
QModelIndex ind = sind;
_modelconv->indexToProxy(ind.model (), ind);
_view->setRootIndex(ind);
}
void Desktopwidget::addToRecent ()
{
QModelIndex index = _dir->menuGetModelIndex ();
QModelIndex src_ind = _dir_proxy->mapToSource(index);
// update the model with this new directory
_model->addToRecent(src_ind);
}
void Desktopwidget::updateSettings ()
{
int count = _model->rowCount (QModelIndex ());
QSettings qs;
qs.remove ("repository");
qs.beginWriteArray ("repository");
for (int i = 1; i < count; i++)
{
QModelIndex index = _model->index (i, 0, QModelIndex ());
qs.setArrayIndex (i - 1);
qs.setValue ("path", _model->data (index, Dirmodel::FilePathRole));
}
qs.endArray ();
}
void Desktopwidget::slotUpdateRepositoryList (QString &dirname, bool add_not_delete)
{
err_info *err = NULL;
if (add_not_delete)
err = addDir (dirname);
else
{
QModelIndex index = _model->index (dirname, 0);
if (index != QModelIndex ())
{
_contents->removeDesk (dirname);
_model->removeDirFromList (index);
_contents->resetDirPath ();
}
else
qDebug () << "slotUpdateRepositoryList: Could not find dirname"
<< dirname << "in model index: ";
}
if (!_main->complain (err))
updateSettings ();
}
void Desktopwidget::slotAddRepository ()
{
QString dir = QFileDialog::getExistingDirectory(this,
tr("Select folder to use as a new repository"));
if (!dir.isEmpty ())
_contents->addRepository (dir);
}
void Desktopwidget::slotRemoveRepository ()
{
QString dir = _dir->menuGetPath ();
_contents->removeRepository (dir);
}
void Desktopwidget::slotRefreshCache()
{
QModelIndex index = _dir->menuGetModelIndex();
QModelIndex src_ind = _dir_proxy->mapToSource(index);
QModelIndex root = _model->findRoot (src_ind);
Operation op ("Refreshing cache", 0, this);
_model->refreshCache(root, &op);
}
void Desktopwidget::deleteDir ()
{
QMessageBox::warning (0, "Paperman",
"Please use the file manager to delete files, then "
"use the refresh option here");
return;
QString path = _dir->menuGetPath ();
QString fullPath;
int ok;
QString oldName = _dir->menuGetName ();
QModelIndex index = _dir->menuGetModelIndex ();
QModelIndex src_ind = _dir_proxy->mapToSource(index);
// find out how many files are in the directory
QString str = _model->countFiles(src_ind, 10);
ok = QMessageBox::question(
this,
tr("Confirmation -- maxview"),
tr("Do you want to delete directory %1 (which contains %2)?")
.arg (path).arg (str),
QMessageBox::Ok, QMessageBox::Cancel);
if ( ok == QMessageBox::Ok)
{
printf ("delete dir\n");
err_info *err;
QDir dir;
qDebug () << "remove dir" << _model->filePath(src_ind);
err = _model->rmdir(src_ind);
if (err)
QMessageBox::warning (0, "Maxview", err->errstr);
}
}
QStringList Desktopwidget::findFolders(const QString& text, QString& dirPath,
QStringList& missing)
{
dirPath = getRootDirectory();
if (dirPath.isEmpty())
return QStringList();
QModelIndex root = getRootIndex();
Operation op ("Scanning folders", 0, this);
return _model->findFolders(text, dirPath, root, missing, &op);
}
void Desktopwidget::startSearch(const QString& path, const QString& match)
{
// Create a 'virtual' maxdesk which holds files from a number different dirs
_contents_proxy->setFilterFixedString ("");
QModelIndex root = getRootIndex();
QString root_path = _model->data(root, QDirModel::FilePathRole).toString ();
Operation op("Scanning folders", 0, this);
QStringList matches;
matches = _model->findFiles(match, path, root, &op);
QModelIndex sind = _contents->finishFileSearch(path, root_path, matches,
_view->getMeasure());
//delete op;
QModelIndex ind = sind;
_modelconv->indexToProxy(ind.model (), ind);
_view->setRootIndex(ind);
// Set the focus so that Escape works
_view->setFocus();
_view->scrollToTop();
}
void Desktopwidget::searchInFolders()
{
Ui::Search ui;
QDialog diag;
_toolbar->setFilterEnabled(false);
ui.setupUi(&diag);
ui.stackName->setText(_search_text);
ui.stackName->setSelection(0, _search_text.size());
QModelIndex index = _dir->menuGetModelIndex ();
QModelIndex src_ind = _dir_proxy->mapToSource(index);
QString path = _model->filePath(src_ind);
ui.folderPath->setText(path);
diag.show();
if (!diag.exec()) {
_toolbar->setFilterEnabled(true);
return;
}
_search_text = ui.stackName->text();
startSearch(path, _search_text);
specialView("Showing the results of folder search");
}
void Desktopwidget::specialView(const QString& prompt)
{
_toolbar->searchLabel->setText(prompt);
_toolbar->setSearchEnabled(true);
_view->setStyleSheet("QListView { background: lightblue; }");
_dir->setEnabled(false);
_main->getMainwindow()->setSearchEnabled(false);
updateActions();
}
void Desktopwidget::normalView()
{
_view->setStyleSheet("QListView { background: lightgray; }");
_dir->setEnabled(true);
_toolbar->setFilterEnabled(true);
_toolbar->setSearchEnabled(false);
_main->getMainwindow()->setSearchEnabled(true);
updateActions();
}
void Desktopwidget::exitSearch()
{
/* This can be called for the Escape key even if there is no search active */
if (!_toolbar->searchEnabled())
return;
normalView();
QModelIndex index = _model->index (_path);
_contents_proxy->setFilterFixedString ("");
QModelIndex root = _model->findRoot (index);
QString root_path = _model->data (root, QDirModel::FilePathRole).toString ();
QModelIndex sind = _contents->showDir(_path, root_path, _view->getMeasure());
QModelIndex ind = sind;
_modelconv->indexToProxy (ind.model (), ind);
_view->setRootIndex (ind);
}
void Desktopwidget::newDir ()
{
QString path = _dir->menuGetPath ();
QString fullPath;
bool ok;
QString text = QInputDialog::getText(
this, "Maxview", "Enter new subdirectory name:", QLineEdit::Normal,
QString(), &ok);
if ( ok && !text.isEmpty() )
{
QModelIndex index = _dir->menuGetModelIndex ();
QModelIndex src_ind = _dir_proxy->mapToSource(index);
// printf ("mkdir %s\n", _model->filePath (index).latin1 ());
QModelIndex new_ind = _model->mkdir(src_ind, text);
// printf (" - got '%s'\n", _model->filePath (index).latin1 ());
if (new_ind == QModelIndex())
QMessageBox::warning(0, "Maxview", "Could not make directory " +
_model->data(src_ind, QDirModel::FilePathRole).toString() + "/" +
text);
}
}
QModelIndex Desktopwidget::findDir(const QString& dir_path)
{
QModelIndex src_ind = _model->index(dir_path);
QModelIndex ind = _dir_proxy->mapFromSource(src_ind);
return ind;
}
bool Desktopwidget::newDir(const QString& dir_path, QModelIndex& index)
{
QDir parent(dir_path);
QString dirname = parent.dirName();
if (!parent.cdUp()) {
// This really cannot happen
QMessageBox::warning(0, "Paperman", "Directory does not exist: " +
parent.path());
return false;
}
qDebug() << "to_create" << dir_path;
QModelIndex parent_ind = _model->index(parent.path(), 0);
QModelIndex src_ind = _model->mkdir(parent_ind, dirname);
if (src_ind == QModelIndex()) {
QMessageBox::warning(0, "Maxview", "Could not make directory " +
parent.path() + "/" + dirname);
return false;
}
index = _dir_proxy->mapFromSource(src_ind);
// Select the directory, since Dirview::menuGetModelIndex() becomes invalid
// when something is added to the proxy model
selectDir(index);
return true;
}
void Desktopwidget::dirSelected(const QModelIndex &index, bool allow_undo,
bool force_change)
{
QModelIndex src_ind = _dir_proxy->mapToSource(index);
QString path = _model->data(src_ind, QDirModel::FilePathRole).toString();
QModelIndex root = _model->findRoot(src_ind);
QString root_path = _model->data(root, QDirModel::FilePathRole).toString();
//qDebug() << "dirSelected" << path << _contents->getDirPath();
// clear the page preview
_page->slotReset ();
// if we have are actually changing directory, do so
if (force_change || path != _contents->getDirPath ())
{
_path = path;
_contents->changeDir (path, root_path, allow_undo);
}
/* otherwise just clear the current selection. This avoid confusion with
keyboard shortcuts which might operate in the directory view and
item view */
else
{
QItemSelectionModel *sel = _view->selectionModel ();
sel->clear ();
}
}
void Desktopwidget::slotDirChanged (QString &dirPath, QModelIndex &deskind)
{
QModelIndex src_ind = _model->index (dirPath);
QModelIndex index = _dir_proxy->mapFromSource(src_ind);
// qDebug () << "Desktopwidget::slotDirChanged" << _dir->currentIndex () << index;
_dir->setCurrentIndex(index);
_modelconv->assertIsSource (0, &deskind, 0);
QModelIndex ind = deskind;
_modelconv->indexToProxy (ind.model (), ind);
_view->setRootIndex (ind);
// ensure that the correct item is displayed
// the filename of the required item is held in _scroll_to
QModelIndex scroll_ind = _contents->index (_scroll_to, deskind);
_modelconv->indexToProxy (scroll_ind.model (), scroll_ind);
_scroll_to = ""; // so we don't do the same next time
if (scroll_ind != QModelIndex ())
{
_view->setSelectionRange (scroll_ind.row (), 1);
_view->scrollTo (scroll_ind);
}
// if no particular 'scroll to' item is specified, just scroll to the last item
else
_view->scrollToLast ();
}
void Desktopwidget::resetFilter()
{
_toolbar->match->clear();
_toolbar->match->setFocus();
}
void Desktopwidget::matchChange(const QString& match)
{
if (!_contents_proxy)
return;
QModelIndex ind;
// update the proxy
// qDebug () << "match" << match;
_contents_proxy->setFilterFixedString (match);
// scroll to the first match
ind = _contents_proxy->index(0, 0, _view->rootIndex ());
if (ind != QModelIndex ())
_view->scrollTo (ind);
}
void Desktopwidget::slotUpdateDone ()
{
emit updateDone ();
}
void Desktopwidget::openStack (const QModelIndex &index)
{
emit showPage (index);
}
void Desktopwidget::updatePreview (void)
{
QModelIndex index = _update_index;
// _page->showPage (_update_index.model (), index);
if (index != QModelIndex())
_page->showPages (_update_index.model(), index, 0, -1, -1);
}
void Desktopwidget::updateActions()
{
// This can be called in the constructor, before things are ready
if (!_act_duplicate)
return;
_view->getSelectionSummary ();
bool at_least_one = _view->isSelection (Desktopview::SEL_at_least_one);
_act_locate->setEnabled(_toolbar->searchEnabled());
_act_stack->setEnabled (_view->isSelection (Desktopview::SEL_more_than_one));
_act_unstack_page->setEnabled (_view->isSelection (Desktopview::SEL_one_multipage));
_act_unstack_all->setEnabled (_view->isSelection (Desktopview::SEL_at_least_one_multipage));
_act_duplicate->setEnabled (at_least_one);
_act_move->setEnabled(_showing_imports && at_least_one);
_act_delete->setEnabled (at_least_one);
_act_rename_stack->setEnabled (at_least_one);
_act_rename_page->setEnabled (_view->isSelection (Desktopview::SEL_one_multipage));
_act_duplicate_page->setEnabled (at_least_one);
_act_duplicate_max->setEnabled (at_least_one);
_act_duplicate_pdf->setEnabled (at_least_one);
_act_duplicate_even->setEnabled (at_least_one);
_act_duplicate_odd->setEnabled (at_least_one);
_act_duplicate_jpeg->setEnabled (at_least_one);
_act_email->setEnabled (at_least_one);
_act_email_max->setEnabled (at_least_one);
_act_email_pdf->setEnabled (at_least_one);
}
void Desktopwidget::slotPopupMenu (QModelIndex &index)
{
_view->setContextIndex (index);
// _contents->slotNewContextEvent (index);
QMenu *context_menu = new QMenu (this);
/* QLabel *caption = new QLabel( "<font color=darkblue><u><b>"
"Stack</b></u></font>", context_menu);
caption->setAlignment( Qt::AlignCenter );*/
//s contextMenu->insertItem( caption );
// get ready to call isSelection()
_view->getSelectionSummary ();
context_menu->addAction (_act_locate);
context_menu->addAction (_act_stack);
context_menu->addAction (_act_unstack_page);
context_menu->addAction (_act_unstack_all);
context_menu->addAction (_act_duplicate);
context_menu->addAction (_act_move);
context_menu->addAction (_act_delete);
context_menu->addAction (_act_rename_stack);
context_menu->addAction (_act_rename_page);
QMenu *submenu = context_menu->addMenu (tr ("&Duplicate..."));
submenu->addAction (_act_duplicate_page);
submenu->addAction (_act_duplicate_max);
submenu->addAction (_act_duplicate_pdf);
submenu->addAction (_act_duplicate_even);
submenu->addAction (_act_duplicate_odd);
submenu->addAction (_act_duplicate_jpeg);
// submenu->insertItem( "as &Tiff", this, SLOT(duplicateTiff()), Qt::CTRL+Qt::Key_T );
submenu = context_menu->addMenu (tr ("&Email..."));