examples/assistant/simpletextviewer/findfiledialog.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the examples of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include <QtGui>
       
    43 
       
    44 #include "findfiledialog.h"
       
    45 
       
    46 //! [0]
       
    47 FindFileDialog::FindFileDialog(QTextEdit *editor, QAssistantClient *assistant,
       
    48                                QWidget *parent)
       
    49     : QDialog(parent)
       
    50 {
       
    51     currentAssistantClient = assistant;
       
    52     currentEditor = editor;
       
    53 //! [0]
       
    54 
       
    55     createButtons();
       
    56     createComboBoxes();
       
    57     createFilesTree();
       
    58     createLabels();
       
    59     createLayout();
       
    60 
       
    61     directoryComboBox->addItem(QDir::toNativeSeparators(QDir::currentPath()));
       
    62     fileNameComboBox->addItem("*");
       
    63     findFiles();
       
    64 
       
    65     setWindowTitle(tr("Find File"));
       
    66 //! [1]
       
    67 }
       
    68 //! [1]
       
    69 
       
    70 void FindFileDialog::browse()
       
    71 {
       
    72     QString currentDirectory = directoryComboBox->currentText();
       
    73     QString newDirectory = QFileDialog::getExistingDirectory(this,
       
    74                                tr("Select Directory"), currentDirectory);
       
    75     if (!newDirectory.isEmpty()) {
       
    76         directoryComboBox->addItem(QDir::toNativeSeparators(newDirectory));
       
    77         directoryComboBox->setCurrentIndex(directoryComboBox->count() - 1);
       
    78         update();
       
    79     }
       
    80 }
       
    81 
       
    82 //! [2]
       
    83 void FindFileDialog::help()
       
    84 {
       
    85     currentAssistantClient->showPage(QLibraryInfo::location(QLibraryInfo::ExamplesPath) +
       
    86             QDir::separator() +  "assistant/simpletextviewer/documentation/filedialog.html");
       
    87 }
       
    88 //! [2]
       
    89 
       
    90 void FindFileDialog::openFile(QTreeWidgetItem *item)
       
    91 {
       
    92     if (!item) {
       
    93         item = foundFilesTree->currentItem();
       
    94         if (!item)
       
    95             return;
       
    96     }
       
    97 
       
    98     QString fileName = item->text(0);
       
    99     QString path = directoryComboBox->currentText() + QDir::separator();
       
   100 
       
   101     QFile file(path + fileName);
       
   102     if (file.open(QIODevice::ReadOnly)) {
       
   103         QString data(file.readAll());
       
   104 
       
   105         if (fileName.endsWith(".html"))
       
   106             currentEditor->setHtml(data);
       
   107         else
       
   108             currentEditor->setPlainText(data);
       
   109     }
       
   110     close();
       
   111 }
       
   112 
       
   113 void FindFileDialog::update()
       
   114 {
       
   115     findFiles();
       
   116     buttonBox->button(QDialogButtonBox::Open)->setEnabled(
       
   117             foundFilesTree->topLevelItemCount() > 0);
       
   118 }
       
   119 
       
   120 void FindFileDialog::findFiles()
       
   121 {
       
   122     QRegExp filePattern(fileNameComboBox->currentText() + "*");
       
   123     filePattern.setPatternSyntax(QRegExp::Wildcard);
       
   124 
       
   125     QDir directory(directoryComboBox->currentText());
       
   126 
       
   127     QStringList allFiles = directory.entryList(QDir::Files | QDir::NoSymLinks);
       
   128     QStringList matchingFiles;
       
   129 
       
   130     foreach (QString file, allFiles) {
       
   131         if (filePattern.exactMatch(file))
       
   132             matchingFiles << file;
       
   133     }
       
   134     showFiles(matchingFiles);
       
   135 }
       
   136 
       
   137 void FindFileDialog::showFiles(const QStringList &files)
       
   138 {
       
   139     foundFilesTree->clear();
       
   140 
       
   141     for (int i = 0; i < files.count(); ++i) {
       
   142         QTreeWidgetItem *item = new QTreeWidgetItem(foundFilesTree);
       
   143         item->setText(0, files[i]);
       
   144     }
       
   145 
       
   146     if (files.count() > 0)
       
   147         foundFilesTree->setCurrentItem(foundFilesTree->topLevelItem(0));
       
   148 }
       
   149 
       
   150 void FindFileDialog::createButtons()
       
   151 {
       
   152     browseButton = new QToolButton;
       
   153     browseButton->setText(tr("..."));
       
   154     connect(browseButton, SIGNAL(clicked()), this, SLOT(browse()));
       
   155 
       
   156     buttonBox = new QDialogButtonBox(QDialogButtonBox::Open
       
   157                                      | QDialogButtonBox::Cancel
       
   158                                      | QDialogButtonBox::Help);
       
   159     connect(buttonBox, SIGNAL(accepted()), this, SLOT(openFile()));
       
   160     connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
       
   161     connect(buttonBox, SIGNAL(helpRequested()), this, SLOT(help()));
       
   162 }
       
   163 
       
   164 void FindFileDialog::createComboBoxes()
       
   165 {
       
   166     directoryComboBox = new QComboBox;
       
   167     fileNameComboBox = new QComboBox;
       
   168 
       
   169     fileNameComboBox->setEditable(true);
       
   170     fileNameComboBox->setSizePolicy(QSizePolicy::Expanding,
       
   171                                     QSizePolicy::Preferred);
       
   172 
       
   173     directoryComboBox->setMinimumContentsLength(30);
       
   174     directoryComboBox->setSizeAdjustPolicy(
       
   175             QComboBox::AdjustToMinimumContentsLength);
       
   176     directoryComboBox->setSizePolicy(QSizePolicy::Expanding,
       
   177                                      QSizePolicy::Preferred);
       
   178 
       
   179     connect(fileNameComboBox, SIGNAL(editTextChanged(const QString &)),
       
   180             this, SLOT(update()));
       
   181     connect(directoryComboBox, SIGNAL(currentIndexChanged(const QString &)),
       
   182             this, SLOT(update()));
       
   183 }
       
   184 
       
   185 void FindFileDialog::createFilesTree()
       
   186 {
       
   187     foundFilesTree = new QTreeWidget;
       
   188     foundFilesTree->setColumnCount(1);
       
   189     foundFilesTree->setHeaderLabels(QStringList(tr("Matching Files")));
       
   190     foundFilesTree->setRootIsDecorated(false);
       
   191     foundFilesTree->setSelectionMode(QAbstractItemView::SingleSelection);
       
   192 
       
   193     connect(foundFilesTree, SIGNAL(itemActivated(QTreeWidgetItem *, int)),
       
   194             this, SLOT(openFile(QTreeWidgetItem *)));
       
   195 }
       
   196 
       
   197 void FindFileDialog::createLabels()
       
   198 {
       
   199     directoryLabel = new QLabel(tr("Search in:"));
       
   200     fileNameLabel = new QLabel(tr("File name (including wildcards):"));
       
   201 }
       
   202 
       
   203 void FindFileDialog::createLayout()
       
   204 {
       
   205     QHBoxLayout *fileLayout = new QHBoxLayout;
       
   206     fileLayout->addWidget(fileNameLabel);
       
   207     fileLayout->addWidget(fileNameComboBox);
       
   208 
       
   209     QHBoxLayout *directoryLayout = new QHBoxLayout;
       
   210     directoryLayout->addWidget(directoryLabel);
       
   211     directoryLayout->addWidget(directoryComboBox);
       
   212     directoryLayout->addWidget(browseButton);
       
   213 
       
   214     QVBoxLayout *mainLayout = new QVBoxLayout;
       
   215     mainLayout->addLayout(fileLayout);
       
   216     mainLayout->addLayout(directoryLayout);
       
   217     mainLayout->addWidget(foundFilesTree);
       
   218     mainLayout->addStretch();
       
   219     mainLayout->addWidget(buttonBox);
       
   220     setLayout(mainLayout);
       
   221 }