summaryrefslogtreecommitdiffstats
path: root/pictureswidget.cpp
blob: 4bc2aadf14274e4a8c7fabdb8ef5a55f23b1a6e5 (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
/*
  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.
*/

#include <QSplitter>
#include <QHBoxLayout>
#include <QSqlQuery>
#include <QSortFilterProxyModel>
#include <QLocale>
#include <QHideEvent>
#include <QHoverEvent>
#include <QEvent>
#include <QHeaderView>
#include <QSettings>
#include <QMessageBox>
#include <QMenu>
#include <QFile>

#include "pictureswidget.h"
#include "mappingtreewidget.h"
#include "smtreeitem.h"
#include "helper.h"
#include "hoverwindow.h"

PicturesWidget::PicturesWidget(QWidget *parent) : QWidget(parent) {
    //setup gui
    QSplitter *splitter = new QSplitter;
    mMappingTree = new MappingTreeWidget;
    mPictureView = new PictureView;
    connect(mMappingTree, SIGNAL(mappingChanged(int)), mPictureView, SLOT(mappingChanged(int)));
    splitter->addWidget(mMappingTree);
    splitter->addWidget(mPictureView);
    splitter->setStretchFactor(0, 1);
    splitter->setStretchFactor(1, 3);

    //put it all togehter
    QHBoxLayout *mainLayout = new QHBoxLayout;
    mainLayout->addWidget(splitter);
    setLayout(mainLayout);
}

PictureView::PictureView(QWidget *parent) : QTreeView(parent) {
    //setup model
    mModel = new PicFilesModel(QStringList() << tr("Filename") << tr("SizeNum") << tr("Format") << tr("Full Path") << tr("Id") << tr("Added") << tr("Md5Sum") << tr("Size"), this);
    mProxy = new QSortFilterProxyModel(this);
    mProxy->setSourceModel(mModel);
    setModel(mProxy);

    //read settings
    QSettings s;
    QByteArray pvHeader = s.value("ui/pvheader").toByteArray();
    mCursorOffset = s.value("ui/cursoroffset").toInt();
    if(!pvHeader.isEmpty()){
        header()->restoreState(pvHeader);
    }

    //hover window
    mHoverWin = new HoverWindow;

    //misc settings
    setSortingEnabled(true);
    setAttribute(Qt::WA_Hover);
    setSelectionMode(QAbstractItemView::ExtendedSelection);
    setAlternatingRowColors(true);
    setColumnHidden(1, true);
    setColumnHidden(4, true);
}

void PictureView::mappingChanged(int mapping){
    mModel->setMapping(mapping);
    mModel->populate();
    for(int i = 0; i < PicFilesModel::NumFields; ++i){
        resizeColumnToContents(i);
    }
}


void PictureView::deletePics(){
    QModelIndexList sel = selectionModel()->selectedRows();
    if(sel.isEmpty()){
        return;
    }
    QString msg = QString(tr("Really delete %1 pics from archive?")).arg(sel.count());
    int retval = QMessageBox::question(this, tr("Question"), msg, QMessageBox::Yes | QMessageBox::No);
    if(retval == QMessageBox::No){
        return;
    }
    QList<QPersistentModelIndex> real;
    foreach(QModelIndex i, sel){
        real << QPersistentModelIndex(mProxy->mapToSource(i));
    }
    mModel->removeFiles(real);
}

void PictureView::hideEvent(QHideEvent *){
    QByteArray pvHeader = header()->saveState();
    QSettings s;
    s.setValue("ui/pvheader", pvHeader);
}

bool PictureView::event(QEvent *e){
    QHoverEvent *hEvent = static_cast<QHoverEvent*>(e);
    if(!hEvent){
        return QTreeView::event(e);
    }
    QPoint hotSpot(hEvent->pos().x(), hEvent->pos().y() + mCursorOffset);
    QModelIndex curIdx = indexAt(hotSpot);
    if(e->type() == QEvent::HoverEnter || e->type() == QEvent::HoverMove){
        if(!curIdx.isValid() || curIdx.column() != PicFilesModel::FileName){
            mHoverWin->setVisible(false);
            mCurHover = QModelIndex();
            return true;
        }
    }
    if(e->type() == QEvent::HoverEnter){
        mCurHover = curIdx;
        QPixmap pm;
        pm.load(mCurHover.data(PicFilesModel::FullPathRole).toString());
        mHoverWin->setPixmap(pm);
        mHoverWin->setPos();
        mHoverWin->setCaption(mCurHover.data().toString());
        mHoverWin->setVisible(true);
        return true;
    }
    if(e->type() == QEvent::HoverMove){
        if(curIdx != mCurHover){
            mCurHover = curIdx;
            QPixmap pm;
            pm.load(mCurHover.data(PicFilesModel::FullPathRole).toString());
            mHoverWin->setPixmap(pm);
            mHoverWin->setPos();
            mHoverWin->setCaption(mCurHover.data().toString());
            mHoverWin->setVisible(true);
            return true;
        }
    }
    return QTreeView::event(e);
}

void PictureView::contextMenuEvent(QContextMenuEvent *e){
    QMenu ctxMenu;
    ctxMenu.addActions(actions());
    ctxMenu.exec(e->globalPos());
}

PicFilesModel::PicFilesModel(const QStringList &headers, QObject *parent) : SmTreeModel(headers, parent) {
    //setup database
    mDb = QSqlDatabase::database("treedb");
    mPopulateQ = new QSqlQuery(mDb);
    mPopulateQ->prepare("SELECT pics.tfilename, pics.isize, pics.tformat, pics.dtadded, pics.cmd5sum, pics.ipicsid FROM pics, pics_mappings WHERE pics_mappings.imapping_id = :mapid AND pics_mappings.ipics_id = pics.ipicsid ORDER BY pics.tfilename");
    mDeleteFileQ = new QSqlQuery(mDb);
    mDeleteFileQ->prepare("DELETE FROM pics WHERE ipicsid = :id");
}

QVariant PicFilesModel::data(const QModelIndex &index, int role) const {
    if(role == Qt::FontRole){
        if(index.column() == Md5Sum){
            return QFont("courier");
        }
    }
    if(role == Qt::ForegroundRole){
        if(index.column() == SizeDisplay){
            int fileSize = index.data(SizeRole).toInt();
            if(fileSize > 1024 * 1024 * 1024){
                return QColor(Qt::red);
            }else{
                return QColor(Qt::green);
            }
        }
    }
    SmTreeItem *item = itemAt(index);
    if(role == FileNameRole){
        return item->data(FileName);
    }
    if(role == SizeRole){
        return item->data(Size);
    }
    if(role == MimeTypeRole){
        return item->data(MimeType);
    }
    if(role == FullPathRole){
        return item->data(FullPath);
    }
    if(role == IdRole){
        return item->data(Id);
    }
    if(role == AddedRole){
        return item->data(Added);
    }
    if(role == Md5SumRole){
        return item->data(Md5Sum);
    }
    if(role == SizeDisplayRole){
        return item->data(SizeDisplay);
    }
    return SmTreeModel::data(index, role);
}

void PicFilesModel::removeFiles(const QList<QPersistentModelIndex> &files){
    foreach(QPersistentModelIndex pi, files){
        mDeleteFileQ->bindValue(":id", pi.data(IdRole));
        if(mDeleteFileQ->exec()){
            QFile::remove(pi.data(FullPathRole).toString());
            removeRows(pi.row(), 1, QModelIndex());
        }
    }
}

void PicFilesModel::populate(){
    SmTreeItem *root = new SmTreeItem(NumFields);
    mPopulateQ->bindValue(":mapid", mMappingId);
    if(mPopulateQ->exec()){
        while(mPopulateQ->next()){
            QList<QVariant> data;
            data << mPopulateQ->value(0) << mPopulateQ->value(1) << mPopulateQ->value(2); // Filename, size and Mime type
            data << Helper::createArchivePath(mPopulateQ->value(0).toString(), mPopulateQ->value(4).toString()); // full path;
            data << mPopulateQ->value(5) << mPopulateQ->value(3) << mPopulateQ->value(4); //Id, added and md5
            QLocale l;
            data << l.toString(mPopulateQ->value(1).toInt());
            SmTreeItem *child = new SmTreeItem(data, root);
            root->appendChild(child);
        }
        setRoot(root);
    }
}