📄 nfolderview.cpp
字号:
#include "nfolderview.h"//#include "adialog.h"#include "ndir.h"//#include <qtopia/qcopenvelope_qws.h>#include <qapplication.h>#include <qcheckbox.h>#include <qfileinfo.h>#include <assert.h>#include <stdio.h>/*! @internal @class NFolderSource nfolderview.h @brief Abstract class that represents a hierarchy of folders. @note A folder can be either a virtual or real directory. But any other files are actually present in the filesystem.*//*! @fn NFolderSource::folder() @brief Returns the current logic folder.*//*! @fn NFolderSource::setFolder(const QString&) @brief Sets the current logic folder.*//*! @fn NFolderSource::isFolder(uint) const @brief Returns whether the entry at index is a logic folder.*//*! @fn NFolderSource::fileAt(uint) const @brief Returns the logic path for a folder and the filesystem path for a file.*/NFolderSource::NFolderSource(QObject *parent, const char *name) : NListSource(parent, name) {}bool NFolderSource::setParentFolder() { return setFolder(folder().left(folder().findRev('/')));}//-------------------------------------------------------///*! @internal @class NFolderView nfolderview.h*/NFolderView::NFolderView(NFolderSource *src, QWidget *parent, const char *name) : NListView(src, parent, name) , m_foldersrc(src) { connect(this, SIGNAL(selected(uint)), SLOT(onSelected(uint))); connect(this, SIGNAL(activated(uint)), SLOT(onActivated(uint))); connect(m_foldersrc, SIGNAL(folderChanged(const QString&)), SLOT(onFolderChanged(const QString&))); connect(this, SIGNAL(changeFolder(const QString&)), m_foldersrc, SLOT(setFolder(const QString&)));}NFolderSource* NFolderView::source() { return m_foldersrc;}void NFolderView::setSource(NFolderSource *src) { assert(src != NULL); disconnect(this, SIGNAL(changeFolder(const QString&)), source(), SLOT(setFolder(const QString&))); connect( this, SIGNAL(changeFolder(const QString&)), src, SLOT(setFolder(const QString&))); NListView::setSource(src); m_foldersrc= src;}void NFolderView::selectAll() { for(int i= 0; i < int(m_foldersrc->count()) - 1; i++) { if(m_foldersrc->indent(i) == m_foldersrc->indent(i + 1)) { setSelection(i, m_foldersrc->count() - i); break; } }}void NFolderView::selectInFilemanager() {// QCopEnvelope e("QPE/Application/afilebrowser", "setDocument(QString)");// e << selectedFile();}void NFolderView::deleteSelection() { deleteFiles(selectedFiles());}QString NFolderView::selectedFile() const { assert(m_selpos < m_foldersrc->count()); return m_foldersrc->fileAt(m_selpos);}QStringList NFolderView::selectedFiles() const { assert(m_selpos + m_selcnt <= m_foldersrc->count()); QStringList res; for(uint i= 0; i < m_selcnt; i++) res+= m_foldersrc->fileAt(m_selpos + i); return res;}bool NFolderView::select(const QString &path) { // set the folder and extract the name int pos= path.findRev('/'); QString folder= path.left(pos); QString name= path.right(path.length() - pos - 1).upper(); if(pos >= 0 && folder != m_foldersrc->folder()) emit changeFolder(folder); // select the nearest entry, ignoring the path entries int best= m_foldersrc->count() - 1; for(uint i= 0; i < m_foldersrc->count(); i++) { if(i + 1 < m_foldersrc->count() && m_foldersrc->indent(i) < m_foldersrc->indent(i + 1)) continue; int res= m_foldersrc->text(i).upper().compare(name); if(res == 0) { setSelection(i); return true; } else if(res > 0) { if(int(i) < best) best= i; } } setSelection(best); return false;}void NFolderView::onSelected(uint index) { assert(index < m_foldersrc->count()); emit selected(m_foldersrc->fileAt(index));}void NFolderView::onActivated(uint index) { assert(index < m_foldersrc->count()); if(m_foldersrc->isFolder(index)) { // on activation of a folder, set it and select the first entry inside emit changeFolder(m_foldersrc->fileAt(index)); } else { emit activated(m_foldersrc->fileAt(index)); }}// select the first entry in the foldervoid NFolderView::onFolderChanged(const QString &path) { // change the selection internally to avoid flicker QStringList pathl= QStringList::split('/', path); setSelection(pathl.count(), 1, false, false); // enforce a full redraw before updating the selection qApp->processOneEvent(); // to ensure the selection is visible ensureSelectionVisible();}void NFolderView::contentsMouseReleaseEvent(QMouseEvent *evt) { assert(selection() < m_foldersrc->count() || selection() == 0); // when pressed on the text of a folder entry if(m_selcnt == 1 && isOnText(evt->pos())) { if(m_foldersrc->isFolder(selection())) { QString path= m_foldersrc->fileAt(selection()); disconnect( m_foldersrc, SIGNAL(folderChanged(const QString &)), this, SLOT(onFolderChanged(const QString &)) ); emit changeFolder(path); connect( m_foldersrc, SIGNAL(folderChanged(const QString &)), this, SLOT(onFolderChanged(const QString &)) ); QStringList pathl= QStringList::split('/', path); setSelection(pathl.count() - 1); } }}void NFolderView::keyPressEvent(QKeyEvent *evt) { if(evt->key() == Key_Left && !evt->isAutoRepeat()) { // go to parent folder QString olddir= m_foldersrc->folder(); disconnect( m_foldersrc, SIGNAL(folderChanged(const QString &)), this, SLOT(onFolderChanged(const QString &)) ); emit changeFolder(m_foldersrc->folder().left(m_foldersrc->folder().findRev('/'))); connect( m_foldersrc, SIGNAL(folderChanged(const QString &)), this, SLOT(onFolderChanged(const QString &)) ); // and select the old folder for(uint i= 0; i < m_foldersrc->count(); i++) { if(m_foldersrc->fileAt(i) == olddir) { setSelection(i); } } } else { NListView::keyPressEvent(evt); }}void NFolderView::deleteFiles(const QStringList &paths) { bool confirmed= false; bool confirmed_all= false; for(uint i= 0; i < paths.count(); i++) { QString path= paths[i]; QFileInfo fi(path); confirmed= confirmed_all; if(!confirmed) { QString message; if(fi.isDir()) { message= tr("<qt>Delete folder <b>%1</b> and all of its subfolders?</qt>").arg(path); } else { message= tr("<qt>Delete file <b>%1</b> ?</qt>").arg(fi.fileName()); }/* ADialog d(qApp->activeWindow(), message, tr("Cancel"), tr("Delete")); QCheckBox *cb= new QCheckBox(tr("Apply to all selected files"), &d, "applybox"); d.addToOptions(cb); if(i == paths.count() - 1) cb->hide(); confirmed= d.exec(); if(cb->isChecked()) { if(confirmed) { confirmed_all= true; } else break; }*/ } if(confirmed) { bool res= NDir::deleteFile(path); if(res && m_selcnt > 1) setSelection(m_selpos, m_selcnt - 1); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -