📄 ndirview.cpp
字号:
#include "ndirview.h"//#include <adialog.h>#include "afiletypes.h"#include <qapplication.h>//#include <qtopia/qcopenvelope_qws.h>//#include <qtopia/timestring.h>#include <qdir.h>#include <qlineedit.h>#include <assert.h>/*! @internal @class NDirSource ndirview.h @brief A NDir that provides extra display options and includes the path in the list.*/NDirSource::NDirSource(QObject *parent, const char *name) : NFolderSource(parent, name) , m_dir(parent) , m_root("/media") , m_hide_suffixes(true) , m_hide_size(false) { connect( &m_dir, SIGNAL(dirChanged(const QString&)), this, SIGNAL(folderChanged(const QString&)) ); connect( &m_dir, SIGNAL(entriesChanged()), this, SLOT(onEntriesChanged()) );}void NDirSource::onEntriesChanged() { emit itemCountChanged(count()); emit itemsChanged(0, count());}QString NDirSource::root() const { return m_root;}void NDirSource::setRoot(const QString &root) { m_root= root; setFolder(folder());}bool NDirSource::suffixesShowing() const { return !m_hide_suffixes;}void NDirSource::showSuffixes(bool enable) { if(m_hide_suffixes == enable) { m_hide_suffixes= !enable; emit itemsChanged(0, count()); }}bool NDirSource::sizeShowing() const { return !m_hide_size;}void NDirSource::showSize(bool enable) { if(m_hide_size == enable) { m_hide_size= !enable; emit itemsChanged(0, count()); }}QString NDirSource::nameFilter() const { return m_dir.nameFilter();}void NDirSource::setNameFilter(const QString &filter) { m_dir.setNameFilter(filter);}bool NDirSource::includesSubdirs() const { return m_dir.includesSubdirs();}void NDirSource::setIncludeSubdirs(bool enable) { m_dir.setIncludeSubdirs(enable);}QString NDirSource::folder() const { return m_dir.path();}bool NDirSource::setFolder(const QString &path) { QDir dir(path); while(!dir.exists()) dir.cdUp(); QString dirpath= dir.absPath(); if(!dirpath.startsWith(m_root)) return false; // create path list, starting with the root entry m_path_entries= QStringList::split('/', dirpath); if(m_path_entries.count() > 1) { QString tpath("/" + m_path_entries[0]); while(m_path_entries.count() > 1) { tpath+= "/" + m_path_entries[1]; if(m_root.contains(tpath)) { m_path_entries.remove(m_path_entries.at(0)); } else { break; } } } return m_dir.setPath(dirpath);}QString NDirSource::nameAt(uint index) const { assert(index < count()); if(index >= count()) return QString(); if(index < m_path_entries.count()) { return m_path_entries[index]; } else { return m_dir.nameAt(index - m_path_entries.count()); }}QString NDirSource::fileAt(uint index) const { assert(index < count()); if(index >= count()) return QString(); if(index < m_path_entries.count()) { QString path= m_root; for(uint i= 1; i <= index; i++) path+= QString("/") + m_path_entries[i]; return path; } else { return m_dir.pathAt(index - m_path_entries.count()); }}bool NDirSource::isFolder(uint index) const { assert(index < count()); if(index >= count()) return false; return index < m_path_entries.count() || m_dir.isDir(fileAt(index));}uint NDirSource::count() const { return m_path_entries.count() + m_dir.count();}QPixmap NDirSource::icon(uint index) const { assert(index < count()); if(index >= count()) return QPixmap(); QPixmap *icon= NULL; QString path= fileAt(index); if(index < m_path_entries.count()) { if(index == 0) { if(path == "/media") { icon= AFileTypes::get()->findIcon("drive"); } else if(path == "/storage") { icon= AFileTypes::get()->findIcon("usb"); } else if(path == "/dev/shm/storage") { icon= AFileTypes::get()->findIcon("usb"); } } else if(index == 1) { if(path.startsWith("/media")) { if(path == "/media/Documents") { icon= AFileTypes::get()->findIcon("documents_open"); } else if(path == "/media/Music") { icon= AFileTypes::get()->findIcon("music_open"); } else if(path == "/media/Photo") { icon= AFileTypes::get()->findIcon("photo_open"); } else if(path == "/media/Video") { icon= AFileTypes::get()->findIcon("video_open"); } } else if(path.startsWith("/dev/shm/storage/") && path.length() == 18) { icon= AFileTypes::get()->findIcon("drive"); } } if(icon == NULL) icon= AFileTypes::get()->findIcon("fopen"); } else if(index < count()) { icon= AFileTypes::get()->findIcon(path); } if(icon != NULL) { return *icon; } else { return QPixmap(); }}int NDirSource::indent(uint index) const { assert(index < count()); return QMIN(index, m_path_entries.count())* 6;}QString NDirSource::text(uint index, uint col) const { assert(index < count()); if(index >= count()) return QString(); QString res; if(col == 0) { // construct filename string (maybe strip suffix) res= nameAt(index); if(index == 0) { if(res == "media") { res= tr("PMA400"); } else if(res == "storage") { res= tr("Ext Device"); } else if(res == "/dev/shm/storage") { res= tr("Ext Device"); } } if(m_hide_suffixes) { int pos= res.findRev('.'); if(pos > 0) { QString suffix= res.right(res.length() - pos).lower(); if(NDir::s_known_suffixes.contains(suffix)) res= res.left(pos); } } } else if(col == 1 && !m_hide_size) { if(isFolder(index)) { return "-"; } else { // construct filesize string QFileInfo fi(fileAt(index)); int size= fi.size(); if(size < 1024) { res= tr("%1 B", "B = Byte").arg(size); } else if(size < 1024*1024) { res= tr("%1 KB", "KB = KByte").arg(size/ 1024); } else if(size < 1024*1024*1024) { res= tr("%1 MB", "MB = MByte").arg(size/ float(1024*1024), 0, 'f', 2); } else { res= tr("%1 GB", "GB = GByte").arg(size/ float(1024*1024*1024), 0, 'f', 2); } } } else if(col == 2 || (col == 1 && m_hide_size)) { if(isFolder(index)) { return "-"; } else { // construct mofification date string QFileInfo fi(fileAt(index)); if(fi.lastModified().daysTo(QDateTime::currentDateTime()) != 0) { //DateFormat d= TimeString::currentDateFormat(); //res= d.numberDate(fi.lastModified().date()); } else { QTime filetime= fi.lastModified().time(); res= filetime.toString().left(5); } } } return res;}//--------------------------------------------------///*! @internal @class NDirView ndirview.h*/NDirView::NDirView(NDirSource *data, QWidget *parent, const char *name) : NFolderView(data, parent, name) , m_dirsrc(data) { connect(this, SIGNAL(selectionEdited(const QString&)), this, SLOT(onSelectionEdited(const QString&)) );}NDirSource* NDirView::source() { return m_dirsrc;}void NDirView::setSource(NDirSource *src) { assert(src != NULL); NFolderView::setSource(src); m_dirsrc= src;}bool NDirView::select(const QString &path) { QString npath= path; if(!m_dirsrc->suffixesShowing()) { int pos= path.findRev("."); if(pos >= 0) npath= npath.left(pos); } return NFolderView::select(npath);}void NDirView::editSelection() { NListView::editSelection(); if(m_ledit != NULL) { // preselect name without suffix QString name= m_ledit->text(); int suffixpos= name.findRev('.'); if(suffixpos <= 0) suffixpos= name.length(); m_ledit->setSelection(0, suffixpos); m_ledit->setCursorPosition(suffixpos); }}void NDirView::onSelectionEdited(const QString &text) { QFileInfo oldinfo(m_dirsrc->fileAt(m_selpos)); QString newname= text; if( !m_dirsrc->suffixesShowing() && NDir::s_known_suffixes.contains(oldinfo.extension().prepend(".")) ) { newname+= "." + oldinfo.extension(); } QFileInfo newinfo(oldinfo.dir(), newname); if(oldinfo.absFilePath() == newinfo.absFilePath()) return; if(newinfo.exists()) {// ADialog d(topLevelWidget(), tr("<qt>A file with this name already exists:<br><b>%1</b></qt>").arg(newinfo.fileName()), tr("Cancel"), tr("Overwrite"));// if(d.exec() == 0) return; } NDir::renameFile(oldinfo.filePath(), newname); m_dirsrc->setFolder(m_dirsrc->folder()); select(newname);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -