📄 kfiledialog.cpp
字号:
#include <unistd.h>#include <stdlib.h>#include <stdio.h>#include <qtimer.h>#include <qtabdialog.h>#include <qstrlist.h>#include <qstring.h>#include <qstack.h>#include <qpushbutton.h>#include <qpopupmenu.h>#include <qpixmap.h>#include <qobjcoll.h>#include <qmessagebox.h>#include <qlistbox.h>#include <qlist.h>#include <qlayout.h>#include <qlabel.h>#include <qcombobox.h>#include <qcolor.h>#include <qcheckbox.h>#include <qbuttongroup.h>#include <qaccel.h>#include <kurl.h>#include <ktoolbar.h>#include <knewpanner.h>#include <kiconloader.h>#include "kfileinfocontents.h"#include "kfiledialogconf.h"#include "kfiledialog.h"#include "kfiledetaillist.h"#include "kfilesimpleview.h"#include "config-kfile.h"#include "kfilepreview.h"#include "kcombiview.h"#include "kdirlistbox.h"#include "kfilefilter.h"#include "kdir.h"#include "kfilebookmark.h"#include <kprocess.h>#include <kapp.h>enum Buttons { BACK_BUTTON= 1000, FORWARD_BUTTON, PARENT_BUTTON, HOME_BUTTON, RELOAD_BUTTON, HOTLIST_BUTTON, PATH_COMBO, MKDIR_BUTTON, FIND_BUTTON, CONFIGURE_BUTTON };enum SortItem { BY_NAME = 1000, BY_SIZE, BY_DATE, BY_OWNER, BY_GROUP };const int idStart = 1;QString *KFileBaseDialog::lastDirectory; // to set the start pathKFileBaseDialog::KFileBaseDialog(const char *dirName, const char *filter, QWidget *parent, const char *name, bool modal, bool acceptURLs) : QDialog(parent, name, modal), boxLayout(0){ QAccel *a = new QAccel( this ); a->connectItem(a->insertItem(Key_T + CTRL), this, SLOT(completion())); // I hard code this for now acceptOnlyExisting = false; wrapper= swallower(); bookmarksMenu= 0; acceptUrls = acceptURLs; if (!lastDirectory) lastDirectory = new QString(QDir::currentDirPath()); dir = new KDir(lastDirectory->data()); visitedDirs = new QStrIList(); // we remember the selected name for init() filename_ = dirName; // For testing connect(dir, SIGNAL(dirEntry(KFileInfo *)), SLOT(slotDirEntry(KFileInfo *))); connect(dir, SIGNAL(newFilesArrived(const KFileInfoList *)), SLOT(insertNewFiles(const KFileInfoList *))); // Init flags finished = true; debugC("start %ld", time(0)); filterString = filter; connect(dir, SIGNAL(finished()), SLOT(slotFinished())); connect(dir, SIGNAL(error(int, const char *)), SLOT(slotKfmError(int, const char *))); backStack.setAutoDelete( true ); forwardStack.setAutoDelete( true );}void KFileBaseDialog::init(){ // Get the config object KConfig *c= kapp->getConfig(); QString oldgroup= c->group(); c->setGroup("KFileDialog Settings"); // // Read configuration from the config file // QWidget *desk = KApplication::desktop(); int w=desk->width() * 5 / 10; int h=desk->height() * 7 / 10; w = c->readNumEntry("Width", w); h = c->readNumEntry("Height", h); showHidden = c->readBoolEntry("ShowHidden", DefaultShowHidden); showStatusLine = c->readBoolEntry("ShowStatusLine", DefaultShowStatusLine); toolbar= new KToolBar(wrapper, "fileDialogToolbar"); // Build the toolbar toolbar->insertButton(kapp->getIconLoader()->loadIcon("back.xpm"), BACK_BUTTON, false, i18n("Back")); toolbar->insertButton(kapp->getIconLoader()->loadIcon("forward.xpm"), FORWARD_BUTTON, false, i18n("Forward")); toolbar->insertButton(kapp->getIconLoader()->loadIcon("up.xpm"), PARENT_BUTTON, true, i18n("Up to parent")); toolbar->insertButton(kapp->getIconLoader()->loadIcon("home.xpm"), HOME_BUTTON, true, i18n("Home")); toolbar->insertButton(kapp->getIconLoader()->loadIcon("reload.xpm"), RELOAD_BUTTON, true, i18n("Reload")); // c->readListEntry("Bookmarks", bookmarks); // bookmarks.insert(0, i18n("Add to bookmarks")); bookmarks= new KFileBookmarkManager(); CHECK_PTR( bookmarks ); connect( bookmarks, SIGNAL( changed() ), this, SLOT( bookmarksChanged() ) ); QString bmFile = KApplication::localkdedir() + "/share/apps/kdeui/"; QDir tmpdir( bmFile ); if ( !tmpdir.exists() ) tmpdir.mkdir(bmFile.data()); bmFile += "bookmarks.html"; bookmarks->read(bmFile); toolbar->insertButton(kapp->getIconLoader()->loadIcon("flag.xpm"), HOTLIST_BUTTON, true, i18n("Bookmarks"), -1); toolbar->insertButton(kapp->getIconLoader()->loadIcon("idea.xpm"), MKDIR_BUTTON, true, i18n("Create new folder")); toolbar->insertButton(kapp->getIconLoader()->loadIcon("search.xpm"), FIND_BUTTON, true, i18n("Search for a file")); toolbar->insertButton(kapp->getIconLoader()->loadIcon("configure.xpm"), CONFIGURE_BUTTON, true, i18n("Configure this dialog [development only]")); connect(toolbar, SIGNAL(clicked(int)), SLOT(toolbarCallback(int))); // try this again connect(toolbar, SIGNAL(pressed(int)), this, SLOT(toolbarPressedCallback(int))); toolbar->insertCombo(i18n("The path"), PATH_COMBO, false, SIGNAL(activated(int)), this, SLOT(comboActivated(int)), true, "Current Path"); toolbar->setFullWidth(true); toolbar->setItemAutoSized (PATH_COMBO); toolbar->setBarPos(KToolBar::Top); toolbar->enableMoving(false); toolbar->adjustSize(); setMinimumWidth(toolbar->width()); if ( c->readBoolEntry("KeepDirsFirst", DefaultKeepDirsFirst) ) dir->setSorting( QDir::Name | QDir::DirsFirst); else dir->setSorting( QDir::Name ); showFilter = getShowFilter(); fileList = initFileList( wrapper ); locationEdit = new KCombo(true, wrapper, "locationedit"); locationEdit->setInsertionPolicy(QComboBox::NoInsertion); locationEdit->setFocus(); locationLabel = new QLabel(locationEdit, i18n("&Location:"), wrapper); locationLabel->adjustSize(); locationEdit->adjustSize(); locationLabel->setMinimumSize(locationLabel->width(), locationEdit->height()); locationEdit->setFixedHeight(locationLabel->height()); connect(locationEdit, SIGNAL(activated(const char*)), SLOT(locationChanged(const char*))); // Add the filter if (showFilter) { filterLabel = new QLabel(i18n("&Filter:"), wrapper); filterLabel->adjustSize(); filterLabel->setMinimumWidth(filterLabel->width()); filterLabel->resize(locationLabel->width(), filterLabel->height()); filterWidget = new KFileFilter(wrapper, "filterwidget"); filterWidget->setFilter(filterString); filterLabel->setBuddy(filterWidget); filterWidget->adjustSize(); filterWidget->setMinimumWidth(100); filterWidget->setFixedHeight(filterWidget->height()); connect(filterWidget, SIGNAL(filterChanged()), SLOT(filterChanged())); hiddenToggle= new QCheckBox(i18n("Show hidden"), this, "hiddentoggle"); hiddenToggle->adjustSize(); hiddenToggle->setChecked(showHidden); hiddenToggle->setMinimumSize(hiddenToggle->size()); connect(hiddenToggle, SIGNAL(toggled(bool)), SLOT(setHiddenToggle(bool))); } // Add the status line if ( showStatusLine ) { myStatusLine = new QLabel( this, "StatusBar" ); myStatusLine->setText("Dir and File Info"); myStatusLine->adjustSize(); myStatusLine->setFrameStyle( QFrame::Panel | QFrame::Sunken ); myStatusLine->setAlignment( AlignHCenter | AlignVCenter ); myStatusLine->setMinimumSize( myStatusLine->sizeHint() ); } else myStatusLine = 0L; bHelp= new QPushButton(i18n("Help"), this, "_help"); bHelp->adjustSize(); bHelp->setMinimumWidth(bHelp->width()); bHelp->setFixedHeight(bHelp->height()); connect(bHelp, SIGNAL(clicked()), SLOT(help())); bOk= new QPushButton(i18n("OK"), this, "_ok"); bOk->adjustSize(); bOk->setMinimumWidth(bOk->width()); bOk->setFixedHeight(bOk->height()); connect(bOk, SIGNAL(clicked()), SLOT(okPressed())); bCancel= new QPushButton(i18n("Cancel"), this, "_cancel"); bCancel->adjustSize(); bCancel->setMinimumWidth(bCancel->width()); bCancel->setFixedHeight(bCancel->height()); connect(bCancel, SIGNAL(clicked()), SLOT(reject())); if (filename_ == "." || filename_.isEmpty()) filename_ = QDir::currentDirPath(); // filename_ is remembered as the dirName argument for the constructor setSelection(filename_); // c->setGroup(oldgroup); // reset the group initGUI(); // activate GM if (showFilter) filterChanged(); else pathChanged(); if (!filename_.isNull()) { debugC("edit %s", locationEdit->text(0)); checkPath(filename_); locationEdit->setText(filename_); } adjustSize(); int w1 = minimumSize().width(); int w2 = toolbar->sizeHint().width() + 10; if (w1 < w2 && w2 < desk->width()) setMinimumWidth(w2); resize(w, h);}void KFileBaseDialog::setFilter(const char *filter){ filterString = filter; if (showFilter) { filterWidget->setFilter(filter); dir->setNameFilter(filterWidget->currentFilter()); pathChanged(); }}void KFileBaseDialog::okPressed(){ filename_ = locationEdit->currentText(); if (!filename_.isNull()) debugC("filename %s", filename_.data()); else { debugC("no filename"); } if (!finished) QApplication::restoreOverrideCursor(); finished = false; accept();}void KFileBaseDialog::initGUI(){ if (boxLayout) delete boxLayout; // deletes all sub layouts boxLayout = new QVBoxLayout(wrapper, 4); // boxLayout->addSpacing(toolbar->height()); boxLayout->addWidget(toolbar, 0); boxLayout->addWidget(fileList->widget(), 4); boxLayout->addSpacing(3); lafBox= new QGridLayout(showFilter ? 2 : 1, 3, 7); boxLayout->addLayout(lafBox, 0); lafBox->addWidget(locationLabel, 0, 0); lafBox->addMultiCellWidget(locationEdit, 0, 0, 1, 2); if (showFilter) { lafBox->addWidget(filterLabel, 1, 0); lafBox->addWidget(filterWidget, 1, 1); lafBox->addWidget(hiddenToggle, 1, 2); } lafBox->setColStretch(0, 1); lafBox->setColStretch(1, 4); lafBox->setColStretch(2, 1); if ( myStatusLine ) boxLayout->addWidget( myStatusLine, 0 ); boxLayout->addSpacing(3); btngroup= new QHBoxLayout(10); boxLayout->addLayout(btngroup, 0); btngroup->addWidget(bHelp, 1); btngroup->addStretch(2); btngroup->addWidget(bOk, 1); btngroup->addWidget(bCancel, 1); boxLayout->activate(); fileList->connectDirSelected(this, SLOT(dirActivated(KFileInfo*))); fileList->connectFileSelected(this, SLOT(fileActivated(KFileInfo*))); fileList->connectFileHighlighted(this, SLOT(fileHighlighted(KFileInfo*)));}KFileBaseDialog::~KFileBaseDialog(){ backStack.clear(); delete bookmarks; delete visitedDirs; delete dir; KConfig *c = kapp->getConfig(); QString oldgroup= c->group(); c->setGroup("KFileDialog Settings"); c->writeEntry("Width", width(), true, true); c->writeEntry("Height", height(), true, true); c->setGroup(oldgroup); c->sync();}void KFileBaseDialog::help() // SLOT{ kapp->invokeHTMLHelp("kfiledialog/index.html", "");}bool KFileDialog::getShowFilter(){ return kapp->getConfig()->readBoolEntry("ShowFilter", DefaultShowFilter);}void KFileBaseDialog::filterChanged() // SLOT{ if (!showFilter || !filterWidget) return; dir->setNameFilter(filterWidget->currentFilter()); pathChanged();}void KFileBaseDialog::setHiddenToggle(bool b) // SLOT{ showHidden = b; pathChanged();}void KFileBaseDialog::locationChanged(const char *_txt){ bool highlighted = strcmp(_txt, locationEdit->text(0)); checkPath(_txt, highlighted);}void KFileBaseDialog::checkPath(const char *_txt, bool takeFiles) // SLOT{ // copy the argument in a temporary string QString text = _txt; // it's unlikely to happen, that at the beginning are spaces, but // for the end, it happens quite often, I guess. text = text.stripWhiteSpace(); // if the argument is no URL (the check is quite fragil) and it's // no absolut path, we add the current directory to get a correct // url if (text.find(':') < 0 && text[0] != '/') text.insert(0, dir->url()); // in case we have a selection defined and someone patched the file- // name, we check, if the end of the new name is changed. if (!selection.isNull()) { int position = text.findRev('/'); ASSERT(position >= 0); // we already inserted the current dir in case QString filename = text.mid(position + 1, text.length()); if (filename != selection) selection = 0; } KURL u = text.data(); // I have to take care of entered URLs bool filenameEntered = false; if (u.isLocalFile()) { // the empty path is kind of a hack KFileInfo i("", u.path()); if (i.isDir()) setDir(text, true); else { if (takeFiles) if (acceptOnlyExisting && !i.isFile()) warning("you entered an invalid URL"); else filenameEntered = true; } } else setDir(text, true); if (filenameEntered) { if (acceptUrls) filename_ = u.url(); else filename_ = u.path(); emit fileSelected(filename_); if (!finished) QApplication::restoreOverrideCursor(); finished = false; accept(); }}QString KFileBaseDialog::selectedFile(){ if (filename_.isNull()) return 0; KURL u = filename_.data(); QString path = u.path(); KURL::decodeURL(path); return path;}QString KFileBaseDialog::dirPath(){ return dir->path();}void KFileBaseDialog::setDir(const char *_pathstr, bool clearforward){ debugC("setDir %s %ld", _pathstr, time(0)); filename_ = 0; QString pathstr = _pathstr; if (pathstr.length() == 0 || pathstr.right(1)[0] != '/') pathstr += "/";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -