/* 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 #include #include #include #include #include #include #include "fileview.h" #include "messagedialog.h" #include "hoverwindow.h" #include "smglobals.h" #include "helper.h" FileView::FileView(QWidget *parent) : QTreeView(parent), mDeleteA(0) { setAttribute(Qt::WA_Hover); 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())); mHoverWin = new HoverWindow; QSize curSize = SmGlobals::instance()->cursorSize(); mHoverWin->setHoverOffset(QPoint(curSize.width() + 30, 0)); readConfig(); } void FileView::markFiles(){ mMarkDialog->show(); } void FileView::unmarkFiles(){ selectionModel()->clearSelection(); } void FileView::createFolder(){ mCreateFolderDialog->show(); } void FileView::readConfig(){ QSettings s; mHover = s.value("ui/hoverpics", true).toBool(); mHoverWin->setWindowOpacity(s.value("ui/hoveropacity", 10).toFloat() / 10.0); } void FileView::doMark(){ int rowCount = model()->rowCount(rootIndex()); QString sRegex = mMarkDialog->text(); if(rowCount && !sRegex.isEmpty()){ QRegExp re(sRegex); QSortFilterProxyModel *proxy = static_cast(model()); QFileSystemModel *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()); QFileSystemModel *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); } } bool FileView::event(QEvent *e){ int eType = e->type(); if((eType == QEvent::HoverEnter) || (eType == QEvent::HoverLeave) || (eType == QEvent::HoverMove)){ QHoverEvent *hEvent = static_cast(e); if(!mHover){ return true; } if(hEvent->type() == QEvent::HoverLeave){ return exitHover(); } QPoint hotSpot(hEvent->pos().x(), hEvent->pos().y() - SmGlobals::instance()->cursorSize().height()); QModelIndex curIdx = indexAt(hotSpot); if(!curIdx.isValid() || (curIdx.column() != 0)){ return exitHover(); } QFileInfo fi(curIdx.data(QFileSystemModel::FilePathRole).toString()); if(!fi.exists()){ return true; } QList hoverData; if(fi.isDir()){ hoverData << fi.fileName(); QDir curDir(fi.absoluteFilePath()); QStringList files = curDir.entryList(QDir::AllEntries | QDir::NoDotAndDotDot, QDir::Name); hoverData << files; }else if(fi.isFile()){ QString mimeType = Helper::mimeType(fi.absoluteFilePath()); if(mimeType.startsWith("image")){ QPixmap pm(fi.absoluteFilePath()); hoverData << pm; }else{ return exitHover(); } }else{ return exitHover(); } if(e->type() == QEvent::HoverEnter){ mCurHover = curIdx; mHoverWin->setData(hoverData); mHoverWin->setPos(); mHoverWin->setVisible(true); return true; } if(e->type() == QEvent::HoverMove){ if(curIdx != mCurHover){ mHoverWin->setData(hoverData); mCurHover = curIdx; mHoverWin->setPos(); mHoverWin->setVisible(true); return true; }else{ mHoverWin->setPos(); return true; } } } return QTreeView::event(e); } bool FileView::exitHover(bool exitVal){ mHoverWin->setVisible(false); mCurHover = QModelIndex(); return exitVal; }