/* 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; } void FileView::markFiles(){ mMarkDialog->show(); } void FileView::unmarkFiles(){ selectionModel()->clearSelection(); } void FileView::createFolder(){ mCreateFolderDialog->show(); } 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){ QSettings s; if(!s.value("ui/hoverpics").toBool()){ return true; } if(e->type() == QEvent::HoverLeave){ mHoverWin->setVisible(false); mCurHover = QModelIndex(); return true; } QHoverEvent *hEvent = static_cast(e); QPoint hotSpot(hEvent->pos().x(), hEvent->pos().y() - SmGlobals::instance()->cursorOffset()); QModelIndex curIdx = indexAt(hotSpot); if(!curIdx.isValid()){ return true; } if(curIdx.column() != 0){ mHoverWin->setVisible(false); return true; } 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{ mHoverWin->setVisible(false); return true; } }else{ mHoverWin->setVisible(false); return true; } QPoint globalPos = mapToGlobal(hotSpot); QPoint where = globalPos + QPoint(30, 0); if(e->type() == QEvent::HoverEnter){ mCurHover = curIdx; mHoverWin->setData(hoverData); if(mHoverWin->pixmapHeight()){ where = QPoint(where.x(), where.y() - mHoverWin->pixmapHeight() / 2); } mHoverWin->setPos(where); mHoverWin->setVisible(true); return true; } if(e->type() == QEvent::HoverMove){ if(curIdx != mCurHover){ mCurHover = curIdx; mHoverWin->setData(hoverData); if(mHoverWin->pixmapHeight()){ where = QPoint(where.x(), where.y() - mHoverWin->pixmapHeight() / 2); } mHoverWin->setVisible(false); mHoverWin->setPos(where); mHoverWin->setVisible(true); return true; }else{ if(mHoverWin->pixmapHeight()){ where = QPoint(where.x(), where.y() - mHoverWin->pixmapHeight() / 2); } mHoverWin->setPos(where); return true; } } return QTreeView::event(e); }