📄 qmdirbrowser.cpp
字号:
/* qmdirbrowser.cpp * * $Id: qmdirbrowser.cpp,v 1.21.2.1 2002/06/20 10:53:43 amos Exp $ * * Apollo sound player: http://www.apolloplayer.org * Copyright(C) 2000-2002 Apollo Team. See CREDITS file. * * 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. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * The GNU General Public License is also available online at: * * http://www.gnu.org/copyleft/gpl.html */#include "qmdirbrowser.h"#include "qmconfig.h"#include "qmdefaultmenuitem.h"#include "qmdirbrowseritem.h"#include "qmmainwindow.h"#include "qmplaylist.h"#include "qmrecoverymanager.h"#include <iostream>#include <qdir.h>#include <qdom.h>#include <qevent.h>#include <qfile.h>#include <qfiledialog.h>#include <qfont.h>#include <qheader.h>#include <qmessagebox.h>#include <qpopupmenu.h>/** * @file qmdirbrowser.cpp * @brief The directory browser widget, presents a directory tree *//*! \class QmDirBrowser qmdirbrowser.h \brief The directory browser wisget, presents a directory tree.*/QmDirBrowser::QmDirBrowser( QmMainWindow *mainwindow, QWidget *parent, const char *) : QmListView(parent, "directorytree"), m_pMainWindow(mainwindow){ enableSelfSave(QmConfig::instance()->configPath() + "directorytree"); addColumn(tr("Dir")); header()->hide(); setRootIsDecorated(true); setSelectionMode(Extended); reorganizable(true); connect(this, SIGNAL(mouseButtonClicked(int, QListViewItem*, const QPoint &, int)), this, SLOT(mouseClick(int, QListViewItem*, const QPoint &, int))); /// \todo Can this be removed? connect(this, SIGNAL(doubleClicked(QListViewItem*)), this, SLOT(appendDir())); connect(this, SIGNAL(rightButtonClicked(QListViewItem*, const QPoint &, int)), this, SLOT(showPopup(QListViewItem*, const QPoint&, int))); if(QmRecoveryManager::instance()->loadingEnabled(QmConfig::DirectoryTree)) loadDirectoryTree(); m_pMenu = new QPopupMenu(this); m_PrependDirId = m_pMenu->insertItem(tr("Add to top"), this, SLOT(prependDir())); QFont f = m_pMenu->font(); m_AppendDirId = m_pMenu->insertItem(new QmDefaultMenuItem(tr("Add to end"), f)); m_pMenu->connectItem(m_AppendDirId, this, SLOT(appendDir())); m_ReplaceDirId = m_pMenu->insertItem(tr("Replace"), this, SLOT(replaceDir())); m_pMenu->insertSeparator(); m_RemoveRootId = m_pMenu->insertItem(tr("Remove"), this, SLOT(removeRoot())); m_pMenu->insertSeparator(); m_pMenu->insertItem(tr("Add new root..."), this, SLOT(addRoot())); m_pMenu->insertItem(tr("Refresh"), this, SLOT(refresh()));}/*! */QmDirBrowser::~QmDirBrowser(){ if( ! fileName().isEmpty()) save();}/*! Called when the user click with the right mouse button on either the playlist tree or directory tree. There's a separate popup menu for the playlist tree and the directory tree. This function also takes care of enabling/disabling the various menu items according to what item is currently highlighted. */voidQmDirBrowser::showPopup( QListViewItem *current, const QPoint &pos, int /*col*/){ bool enable; if(current == 0) { m_pMenu->setItemEnabled(m_RemoveRootId, false); enable = false; } else { enable = true; if(current->depth() > 0) m_pMenu->setItemEnabled(m_RemoveRootId, false); else m_pMenu->setItemEnabled(m_RemoveRootId, true); } m_pMenu->setItemEnabled(m_PrependDirId, enable); m_pMenu->setItemEnabled(m_AppendDirId, enable); m_pMenu->setItemEnabled(m_ReplaceDirId, enable); m_pMenu->popup(pos);}/*! Called when either of the mouse buttons are clicked. \sa showPopup(QListViewItem*, const QPoint&, int) */voidQmDirBrowser::mouseClick( int button, QListViewItem *item, const QPoint &pos, int c){ if (QmMainWindow::mainwin->state() == QmMainWindow::Ready) { QmMainWindow::mainwin->setState(QmMainWindow::Busy); if (button & Qt::MidButton) { replaceDir(); } else if (button & Qt::RightButton) showPopup(item, pos, c); QmMainWindow::mainwin->setState(QmMainWindow::Ready); }}// void// QmDirBrowser::mouseClick(// int button,// QListViewItem *,// const QPoint &,// int )// {// if ( button & Qt::MidButton )// replaceDir();// // else if ( button & Qt::RightButton )// // showPopup(item, pos, c);// // else if ( button & Qt::LeftButton )// // {// // if(item == 0 && m_pRenameEdit->isVisible())// // m_pRenameEdit->hide();// // }// }/*! Opens up a dialog for the user to define (add) a new root in the directory browser.*/voidQmDirBrowser::addRoot(){ QString name = QFileDialog::getExistingDirectory(QString::null, this, "root-selector", tr("Add root"), true); if(name.isEmpty()) return; QDir d(name); if( ! d.isReadable()) { QMessageBox::information(this, tr("Apollo - Add root?"), tr("The directory is not readable or does not exists.")); return; } (void) new QmDirBrowserItem(this, name, name);}/*! Re-reads the directory.*/voidQmDirBrowser::refresh(){ QmDirBrowserItem *current = static_cast<QmDirBrowserItem*>(currentItem()); if (current == 0) return; bool open = current->isOpen(); current->setOpen(false); QString file = current->fileName(); QString text = current->text(0); QListViewItem *parent = current->parent(); if (parent) { parent->takeItem(current); delete current; current = new QmDirBrowserItem(static_cast<QmDirBrowserItem*>(parent), text, file); } else { takeItem(current); delete current; current = new QmDirBrowserItem(this, text, file); } if (open) current->setOpen(true);}/*! Removes the selected root, if any. If a subdirectory is selected, this function does nothing.*/voidQmDirBrowser::removeRoot(){ QListViewItem *current = currentItem(); if(current == 0 || current->depth() > 0) return; delete current;}/*! The highlighted directories in the directory tree will be appended to the playlist. \sa prependDir(), replaceDir() */voidQmDirBrowser::appendDir(){ QList<QListViewItem> *items = selectedItems(); for (QListViewItem *i=items->first(); i; i = items->next()) { QmMainWindow::mainwin->playListAppendDir(reinterpret_cast<QmDirBrowserItem *>(i)->fileName()); } delete items;}/*! The highlighted directories in the directory tree will be prepended to the playlist. \sa appendDir(), replaceDir() */voidQmDirBrowser::prependDir(){ QList<QListViewItem> *items = selectedItems(); for (QListViewItem *i=items->last(); i; i = items->prev()) { QmMainWindow::mainwin->playListPrependDir(reinterpret_cast<QmDirBrowserItem *>(i)->fileName()); } delete items;}/*! The highlighted directories in the directory tree will replace the playlist. That is, the playlist will be cleared and then these directories added. \sa appendDir(), prependDir()*/voidQmDirBrowser::replaceDir(){ QmMainWindow::mainwin->clearPlayList(); appendDir();}/*! Loads the directory tree.*/voidQmDirBrowser::loadDirectoryTree(){ QDomDocument doc( "apollo-directorytree" ); QFile f( QmConfig::instance()->configPath() + "directorytree" ); if ( ! f.open( IO_ReadOnly ) ) return; if ( ! doc.setContent( &f ) ) { f.close(); return; } f.close(); QDomElement root = doc.documentElement(); if(root.nodeName() != "apollo-directorytree") { qWarning("QmDirBrowser::loadDirectoryTree(): Invalid root node."); return; } // Get the first child in the document. This should be a 'dir' element. QDomNode n = root.firstChild(); while( ! n.isNull() ) { // Get the 'dir' element. QDomElement e = n.toElement(); if( ! e.isNull() ) { if(e.tagName() != "dir") qWarning("QmDirBrowser::loadDirectoryTree(): Invalid element."); else { // We have the 'dir' element. Read the values it contains, i.e. // its children's values. QDomNode node = e.firstChild(); QDomNode data; QString text, path, open; while( ! node.isNull()) { data = node.firstChild(); if(node.nodeName() == "name") text = data.nodeValue(); else if(node.nodeName() == "title") text = data.nodeValue(); else if(node.nodeName() == "path") path = data.nodeValue(); else if(node.nodeName() == "open") open = data.nodeValue(); node = node.nextSibling(); } QmDirBrowserItem *i = new QmDirBrowserItem(this, text, path); if(open == "1") i->setOpen(true); } } n = n.nextSibling(); }}// note: identical code in qmplaylistbrowser - modify both voidQmDirBrowser::startDragging(){ if (!reorganizable()) return; QStringList files; QList<QListViewItem> *selected = selectedItems(); for (QListViewItem *i=selected->first(); i; i=selected->next()) files.append(reinterpret_cast<QmBrowserItem*>(i)->fileName()); delete selected; for ( QStringList::Iterator it = files.begin(); it != files.end(); ++it ) { printf( "%s \n", (*it).latin1() ); } QUriDrag *d = new QUriDrag(this); d->setFilenames(files); d->dragMove();}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -