diff options
author | Arno <arno@disconnect.de> | 2016-08-20 17:04:58 +0200 |
---|---|---|
committer | Arno <arno@disconnect.de> | 2016-08-20 17:04:58 +0200 |
commit | 96c798d4e9ca91cade25aa6d38176714979685bc (patch) | |
tree | 0a9293bd203e68faeb28e668fe5e8ab32ec5c1b5 /shemovcleaner.cpp | |
parent | ddd1616f5c310c94214e0ba2629f174c4cf768f3 (diff) | |
download | ShemovCleaner-96c798d4e9ca91cade25aa6d38176714979685bc.tar.gz ShemovCleaner-96c798d4e9ca91cade25aa6d38176714979685bc.tar.bz2 ShemovCleaner-96c798d4e9ca91cade25aa6d38176714979685bc.zip |
Revamped ShemovCleaner to a QTabWidget
It's a rather large commit. After renaming ShemovCleaner to
TorrentWidget, I had to recreate ShemovCleaner as MainWindow with a
single Tab.
Then I created QActions for everything, including a QToolBar, a QMenu
and a contetext Menu. For that the button bar at the bottom had to go.
Oh, and I added some icons for the actions!
Diffstat (limited to 'shemovcleaner.cpp')
-rw-r--r-- | shemovcleaner.cpp | 324 |
1 files changed, 71 insertions, 253 deletions
diff --git a/shemovcleaner.cpp b/shemovcleaner.cpp index b33f526..fb9b9e6 100644 --- a/shemovcleaner.cpp +++ b/shemovcleaner.cpp @@ -1,112 +1,40 @@ -#include <QSqlDatabase> -#include <QSqlQuery> -#include <QMessageBox> -#include <QGridLayout> -#include <QPushButton> -#include <QFileDialog> -#include <QDir> -#include <QLineEdit> -#include <QTreeView> -#include <QStandardItemModel> -#include <QStandardItem> -#include <QFileInfo> -#include <QBrush> +#include <QTabWidget> #include <QLabel> -#include <QDateTime> -#include <QSettings> -#include <QHeaderView> #include <QStatusBar> +#include <QAction> +#include <QToolBar> +#include <QMenu> +#include <QMenuBar> #include <QApplication> -#include <QRegularExpression> - -#include <QDebug> #include "shemovcleaner.h" -#include "filesorter.h" -#include "torrentparser.h" -#include "torrentdisplay.h" - -ShemovCleaner::ShemovCleaner(QWidget *parent) : QMainWindow(parent), mExt("*.torrent") { - QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL", "shemovdb"); - db.setHostName("hadante.d-tor.org"); - db.setUserName("shemov"); - db.setPassword("shemov"); - db.setDatabaseName("shemov2"); - if(!db.open()){ - QMessageBox::critical(0, tr("Error"), tr("Could not open database!")); - } +#include "torrentwidget.h" - setupGui(); - gatherData(); -} - -ShemovCleaner::~ShemovCleaner(){ - writeHeaderData(); - writeSettings(); -} - -void ShemovCleaner::setupGui(){ +ShemovCleaner::ShemovCleaner(QWidget *parent, Qt::WindowFlags f) : QMainWindow(parent, f) { + //general setup qApp->setWindowIcon(QIcon(":/clean_tampon.png")); - mDir = new QLineEdit; - mSelDir = new QPushButton(tr("Browse...")); - connect(mSelDir, SIGNAL(clicked()), this, SLOT(selectDir())); - - mSearchTorrents = new QLineEdit; - connect(mSearchTorrents, SIGNAL(returnPressed()), this, SLOT(searchFile())); - mDoSearchTorrents = new QPushButton(tr("Search")); - connect(mDoSearchTorrents, SIGNAL(clicked()), this, SLOT(searchFile())); - - QGridLayout *buttonL = new QGridLayout; - buttonL->addWidget(new QLabel(tr("Directory")), 0, 0); - buttonL->addWidget(mDir, 0, 1); - buttonL->addWidget(mSelDir, 0, 2); - buttonL->addWidget(new QLabel(tr("Search file")), 1, 0); - buttonL->addWidget(mSearchTorrents, 1, 1); - buttonL->addWidget(mDoSearchTorrents, 1, 2); - - mFileView = new QTreeView; - mFileView->setSortingEnabled(true); - mFileView->selectionModel(); - mFileView->setSelectionBehavior(QAbstractItemView::SelectRows); - mFileView->setSelectionMode(QAbstractItemView::ExtendedSelection); - - mModel = new QStandardItemModel; - mProxy = new FileSorter; - mProxy->setSourceModel(mModel); - mFileView->setModel(mProxy); - QLabel *fileL = new QLabel(tr("Found:")); - readSettings(); - - mDelete = new QPushButton(tr("Delete...")); - connect(mDelete, SIGNAL(clicked()), this, SLOT(deleteFiles())); - mMove = new QPushButton(tr("Move...")); - connect(mMove, SIGNAL(clicked()), this, SLOT(moveFiles())); - mInfo = new QPushButton(tr("Torrent...")); - connect(mInfo, SIGNAL(clicked()), this, SLOT(torrentInfo())); - QHBoxLayout *buttonL2 = new QHBoxLayout; - buttonL2->addStretch(); - buttonL2->addWidget(mMove); - buttonL2->addWidget(mDelete); - buttonL2->addWidget(mInfo); - buttonL2->addStretch(); - - QWidget *mainWidget = new QWidget; - QVBoxLayout *mainLayout = new QVBoxLayout; - mainLayout->addLayout(buttonL); - mainLayout->addWidget(fileL); - mainLayout->addWidget(mFileView); - mainLayout->addLayout(buttonL2); - mainWidget->setLayout(mainLayout); setMinimumWidth(800); setMinimumHeight(600); - setCentralWidget(mainWidget); + mTorrentTab = new TorrentWidget; + mTab = new QTabWidget; + mTab->addTab(mTorrentTab, tr("Torrents")); + setCentralWidget(mTab); + + createStatusBar(); + createActions(); + connect(mTorrentTab, SIGNAL(statusMessage(QString)), this, SLOT(statusBarMessage(QString))); + connect(mTorrentTab, SIGNAL(selectionCountChanged(QString)), this, SLOT(setSelectionCount(QString))); +} - createStatusbar(); - mTorrentDisplay = new TorrentDisplay(this); - connect(mFileView->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), this, SLOT(fileSelectionChanged(QItemSelection,QItemSelection))); +void ShemovCleaner::statusBarMessage(const QString &msg){ + statusBar()->showMessage(msg); } -void ShemovCleaner::createStatusbar(){ +void ShemovCleaner::setSelectionCount(const QString &msg){ + mSelected->setText(msg); +} + +void ShemovCleaner::createStatusBar(){ QLabel *l1 = new QLabel(tr("Sel.")); mSelected = new QLabel("000/000"); mSelected->setFrameStyle(QFrame::Panel | QFrame::Sunken); @@ -114,162 +42,52 @@ void ShemovCleaner::createStatusbar(){ statusBar()->addPermanentWidget(mSelected); } -void ShemovCleaner::gatherData(){ - QSqlDatabase db = QSqlDatabase::database("shemovdb"); - QSqlQuery q(db); - q.prepare("SELECT COUNT(*) FROM metadata WHERE tsubject = :fn"); - QDir d(mDir->text()); - QFileInfoList fl = d.entryInfoList(QStringList() << mExt, QDir::Files, QDir::Name); - mModel->clear(); - QStandardItem *root = mModel->invisibleRootItem(); - mModel->setHorizontalHeaderLabels(QStringList() << QChar(0x26A7) << tr("Name") << tr("Created")); - QBrush redBrush(Qt::red); - QBrush greenBrush(Qt::darkGreen); - foreach(QFileInfo fi, fl){ - int result = 0; - q.bindValue(":fn", fi.fileName()); - q.exec(); - while(q.next()){ - result = q.value(0).toInt(); - } - QStandardItem *i1; - QStandardItem *i2 = new QStandardItem(fi.fileName()); - QStandardItem *i3 = new QStandardItem(fi.created().toString(Qt::RFC2822Date)); - if(result){ - i1 = new QStandardItem(QIcon(":/huge_bra.png"), QString()); - i1->setData(1); - i2->setForeground(greenBrush); - }else{ - i1 = new QStandardItem(QIcon(":/gaping_ass.png"), QString()); - i1->setData(0); - i2->setForeground(redBrush); - } - i2->setData(fi.absoluteFilePath()); - root->appendRow(QList<QStandardItem*>() << i1 << i2 << i3); - } - readHeaderData(); -} - -void ShemovCleaner::deleteFiles(){ - QModelIndexList sel = mFileView->selectionModel()->selectedRows(1); - if(sel.isEmpty()){ - return; - } - QString q = QString(tr("Really delete %1 file(s)?")).arg(QString::number(sel.count())); - int res = QMessageBox::question(this, tr("Delete files..."), q); - int ctr = 0; - if(res == QMessageBox::Yes){ - foreach(QModelIndex i, sel){ - QString fp = i.data(Qt::UserRole + 1).toString(); - bool ok = QFile::remove(fp); - if(ok){ - ++ctr; - }else{ - QFileInfo fi(fp); - QString msg = QString(tr("Shit: %1 vanished from under us!")).arg(fi.fileName()); - QMessageBox::critical(this, tr("Error"), msg); - } - } - QString msg = QString(tr("Successfully deleted %1 of %2 file(s)")).arg(QString::number(ctr)).arg(QString::number(sel.count())); - statusBar()->showMessage(msg); - gatherData(); - } -} - -void ShemovCleaner::moveFiles(){ - QModelIndexList sel = mFileView->selectionModel()->selectedRows(1); - if(sel.isEmpty()){ - return; - } - QString dir = QFileDialog::getExistingDirectory(this, tr("Select directory"), QDir::homePath()); - if(!dir.isEmpty()){ - int ctr = 0; - foreach(QModelIndex i, sel){ - QFileInfo fp(i.data(Qt::UserRole + 1).toString()); - QString newfn = QString("%1/%2").arg(dir).arg(fp.fileName()); - bool ok = QFile::rename(fp.absoluteFilePath(), newfn); - if(ok){ - ++ctr; - } - } - QString msg = QString(tr("Successfully moved %1 of %2 file(s)")).arg(QString::number(ctr)).arg(QString::number(sel.count())); - statusBar()->showMessage(msg); - gatherData(); - } -} - -void ShemovCleaner::torrentInfo(){ - QModelIndexList sel = mFileView->selectionModel()->selectedRows(1); - if(sel.isEmpty()){ - return; - } - QString fn = sel.first().data(Qt::UserRole + 1).toString(); - TorrentParser p(fn, this); - QList<QVariant> tData = p.parse(); - tData.removeAll(QVariant()); - tData.removeAll(0); - mTorrentDisplay->setData(tData, sel.first().data().toString()); - mTorrentDisplay->show(); - return; -} - -void ShemovCleaner::searchFile(){ - mFileView->selectionModel()->clear(); - int count = mProxy->rowCount(); - QRegularExpression regex(mSearchTorrents->text(), QRegularExpression::CaseInsensitiveOption); - int success = 0; - for(int i = 0; i < count; ++i){ - QModelIndex cur = mProxy->index(i, 1); - QString fn = cur.data(Qt::UserRole + 1).toString(); - TorrentParser p(fn); - statusBar()->showMessage(QString(tr("parsing %1").arg(fn))); - QList<QVariant> torrentData = p.parse(); - QStringList files = p.files(torrentData); - foreach(QString f, files){ - QRegularExpressionMatch m = regex.match(f); - if(m.hasMatch()){ - ++success; - mFileView->selectionModel()->select(cur, QItemSelectionModel::Select | QItemSelectionModel::Rows); - } - } - } - statusBar()->showMessage(QString(tr("Found %1 file(s)").arg(QString::number(success)))); -} - -void ShemovCleaner::selectDir(){ - QString dir = QFileDialog::getExistingDirectory(this, tr("Select directory"), QDir::homePath()); - mDir->setText(QDir::toNativeSeparators(dir)); -} - -void ShemovCleaner::fileSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected){ - Q_UNUSED(selected); - Q_UNUSED(deselected); - int count = mFileView->selectionModel()->selectedRows().count(); - int total = mModel->rowCount(); - QString msg = QString("%1/%2").arg(QString::number(count)).arg(QString::number(total)); - mSelected->setText(msg); -} - -void ShemovCleaner::readSettings(){ - QSettings s; - QString dir = s.value("searchdir", QDir::toNativeSeparators(QDir::homePath())).toString(); - mDir->setText(dir); -} - -void ShemovCleaner::writeSettings(){ - QSettings s; - s.setValue("searchdir", mDir->text()); -} - -void ShemovCleaner::readHeaderData(){ - QSettings s; - QByteArray headers = s.value("fileheaders").toByteArray(); - if(!headers.isEmpty()){ - mFileView->header()->restoreState(headers); - } +void ShemovCleaner::createActions(){ + //Application + QAction *quitA = new QAction(tr("Quit"), this); + connect(quitA, SIGNAL(triggered()), qApp, SLOT(closeAllWindows())); + //TorrentWidget + mTorRefreshA = new QAction(QIcon(":/refresh.png"), tr("Refresh"), this); + connect(mTorRefreshA, SIGNAL(triggered()), mTorrentTab, SLOT(gatherData())); + mTorDeleteA = new QAction(QIcon(":/delete.png"), tr("Delete..."), this); + connect(mTorDeleteA, SIGNAL(triggered()), mTorrentTab, SLOT(deleteFiles())); + mTorMoveA = new QAction(QIcon(":/diaper.png"), tr("Move..."), this); + connect(mTorMoveA, SIGNAL(triggered()), mTorrentTab, SLOT(moveFiles())); + mTorInfoA = new QAction(QIcon(":/huge_bra.png"), tr("Torrent info..."), this); + connect(mTorInfoA, SIGNAL(triggered()), mTorrentTab, SLOT(torrentInfo())); + mTorDirA = new QAction(QIcon(":/folder.png"), tr("Select folder..."), this); + connect(mTorDirA, SIGNAL(triggered()), mTorrentTab, SLOT(selectDir())); + mTorrentTab->toolBar()->addAction(mTorRefreshA); + mTorrentTab->toolBar()->addSeparator(); + mTorrentTab->toolBar()->addAction(mTorDirA); + mTorrentTab->toolBar()->addAction(mTorMoveA); + mTorrentTab->toolBar()->addAction(mTorDeleteA); + mTorrentTab->toolBar()->addSeparator(); + mTorrentTab->toolBar()->addAction(mTorInfoA); + QMenu *torFileM = new QMenu(tr("File")); + torFileM->addAction(mTorDirA); + torFileM->addAction(mTorRefreshA); + torFileM->addSeparator(); + torFileM->addAction(quitA); + mTorrentTab->menuBar()->addMenu(torFileM); + QMenu *torEditM = new QMenu(tr("Edit")); + torEditM->addAction(mTorDeleteA); + torEditM->addAction(mTorMoveA); + torEditM->addSeparator(); + torEditM->addAction(mTorInfoA); + mTorrentTab->menuBar()->addMenu(torEditM); + mTorrentTab->addAction(mTorInfoA); + mTorrentTab->addAction(createSeparator()); + mTorrentTab->addAction(mTorRefreshA); + mTorrentTab->addAction(createSeparator()); + mTorrentTab->addAction(mTorMoveA); + mTorrentTab->addAction(mTorDeleteA); + mTorrentTab->addAction(createSeparator()); + mTorrentTab->addAction(mTorDirA); } -void ShemovCleaner::writeHeaderData(){ - QSettings s; - s.setValue("fileheaders", mFileView->header()->saveState()); +QAction *ShemovCleaner::createSeparator(){ + QAction *retval = new QAction(this); + retval->setSeparator(true); + return retval; } |