/* 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 #include #include #include #include #include #include #include #include #include #include "fileview.h" #include "messagedialog.h" FileView::FileView(QWidget *parent) : QTreeView(parent), mDeleteA(0) { setRootIsDecorated(false); QString title = QString("%1 - %2"); QString markTitle = title.arg(qApp->applicationName(), tr("Mark files")); mMarkDialog = new MessageDialog(tr("Enter pattern to mark"), markTitle, this); connect(mMarkDialog, SIGNAL(accepted()), this, SLOT(doMark())); QString folderTitle = title.arg(qApp->applicationName(), tr("Create folder")); mCreateFolderDialog = new MessageDialog(tr("Enter folder name"), folderTitle, this); connect(mCreateFolderDialog, SIGNAL(accepted()), this, SLOT(doCreateFolder())); } void FileView::markFiles(){ mMarkDialog->show(); } void FileView::unmarkFiles(){ selectionModel()->clearSelection(); } void FileView::createFolder(){ mCreateFolderDialog->show(); } void FileView::refresh(){ QSortFilterProxyModel *proxy = static_cast(model()); QDirModel *model = static_cast(proxy->sourceModel()); QModelIndex root = rootIndex(); QModelIndex real = proxy->mapToSource(root); model->refresh(root); } void FileView::doMark(){ int rowCount = model()->rowCount(rootIndex()); QString sRegex = mMarkDialog->text(); if(rowCount && !sRegex.isEmpty()){ QRegExp re(sRegex); QSortFilterProxyModel *proxy = static_cast(model()); QDirModel *model = static_cast(proxy->sourceModel()); bool match(false); for(int i = 0; i < rowCount; ++i){ QModelIndex cur = rootIndex().child(i, 0); QModelIndex sCur = proxy->mapToSource(cur); if(model->isDir(sCur)){ continue; } if(re.indexIn(cur.data().toString()) != -1){ selectionModel()->select(cur, QItemSelectionModel::Select | QItemSelectionModel::Rows); match = true; } } if(match){ statusbarMessage(QString()); }else{ emit statusbarMessage(tr("No match found!")); } }else{ emit statusbarMessage(tr("Nothing to mark!")); } } void FileView::doCreateFolder(){ QString folderName = mCreateFolderDialog->text(); if(folderName.isEmpty()){ emit statusbarMessage(tr("No foldername given!")); return; } QSortFilterProxyModel *proxy = static_cast(model()); QDirModel *model = static_cast(proxy->sourceModel()); QModelIndex sRoot = proxy->mapToSource(rootIndex()); QModelIndex newIdx = model->mkdir(sRoot, folderName); if(newIdx == QModelIndex()){ QString msg = QString(tr("Failed to create %1/%2")).arg(model->filePath(sRoot)).arg(folderName); emit statusbarMessage(msg); }else{ QString msg = QString(tr("Created folder %1")).arg(model->filePath(newIdx)); emit statusbarMessage(msg); } } void FileView::contextMenuEvent(QContextMenuEvent *e){ QMenu contextMenu(this); foreach(QAction *a, actions()){ contextMenu.addAction(a); } contextMenu.exec(e->globalPos()); } void FileView::keyPressEvent(QKeyEvent *e){ switch(e->key()){ case Qt::Key_Backspace: emit upDir(); e->accept(); break; case Qt::Key_Enter: case Qt::Key_Return: emit enterPressed(currentIndex()); e->accept(); break; case Qt::Key_Delete: emit delFiles(); e->accept(); break; default: QTreeView::keyPressEvent(e); } } void FileView::resizeEvent(QResizeEvent *e){ if(e->size().width() != e->oldSize().width()){ int width = e->size().width(); int c1width = width / 2; // * 2; setColumnWidth(0, c1width); } }