Sophie

Sophie

distrib > Mandriva > current > i586 > media > main-updates > by-pkgid > 8e6051afcdb111a0317a58fb64c2abf5 > files > 654

qt4-doc-4.6.3-0.2mdv2010.2.i586.rpm

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Qt 4.6: window.cpp Example File (dialogs/findfiles/window.cpp)</title>
  <link href="classic.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="left" valign="top" width="32"><a href="http://qt.nokia.com/"><img src="images/qt-logo.png" align="left" border="0" /></a></td>
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">All&nbsp;Functions</font></a>&nbsp;&middot; <a href="overviews.html"><font color="#004faf">Overviews</font></a></td></tr></table><h1 class="title">window.cpp Example File<br /><span class="small-subtitle">dialogs/findfiles/window.cpp</span>
</h1>
<pre><span class="comment"> /****************************************************************************
 **
 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
 ** All rights reserved.
 ** Contact: Nokia Corporation (qt-info@nokia.com)
 **
 ** This file is part of the examples of the Qt Toolkit.
 **
 ** $QT_BEGIN_LICENSE:LGPL$
 ** Commercial Usage
 ** Licensees holding valid Qt Commercial licenses may use this file in
 ** accordance with the Qt Commercial License Agreement provided with the
 ** Software or, alternatively, in accordance with the terms contained in
 ** a written agreement between you and Nokia.
 **
 ** GNU Lesser General Public License Usage
 ** Alternatively, this file may be used under the terms of the GNU Lesser
 ** General Public License version 2.1 as published by the Free Software
 ** Foundation and appearing in the file LICENSE.LGPL included in the
 ** packaging of this file.  Please review the following information to
 ** ensure the GNU Lesser General Public License version 2.1 requirements
 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 **
 ** In addition, as a special exception, Nokia gives you certain additional
 ** rights.  These rights are described in the Nokia Qt LGPL Exception
 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
 **
 ** GNU General Public License Usage
 ** Alternatively, this file may be used under the terms of the GNU
 ** General Public License version 3.0 as published by the Free Software
 ** Foundation and appearing in the file LICENSE.GPL included in the
 ** packaging of this file.  Please review the following information to
 ** ensure the GNU General Public License version 3.0 requirements will be
 ** met: http://www.gnu.org/copyleft/gpl.html.
 **
 ** If you have questions regarding the use of this file, please contact
 ** Nokia at qt-info@nokia.com.
 ** $QT_END_LICENSE$
 **
 ****************************************************************************/</span>

 #include &lt;QtGui&gt;

 #include &quot;window.h&quot;

 Window::Window(QWidget *parent)
     : QDialog(parent)
 {
     browseButton = createButton(tr(&quot;&amp;Browse...&quot;), SLOT(browse()));
     findButton = createButton(tr(&quot;&amp;Find&quot;), SLOT(find()));

     fileComboBox = createComboBox(tr(&quot;*&quot;));
     textComboBox = createComboBox();
     directoryComboBox = createComboBox(QDir::currentPath());

     fileLabel = new QLabel(tr(&quot;Named:&quot;));
     textLabel = new QLabel(tr(&quot;Containing text:&quot;));
     directoryLabel = new QLabel(tr(&quot;In directory:&quot;));
     filesFoundLabel = new QLabel;

     createFilesTable();

     QHBoxLayout *buttonsLayout = new QHBoxLayout;
     buttonsLayout-&gt;addStretch();
     buttonsLayout-&gt;addWidget(findButton);

     QGridLayout *mainLayout = new QGridLayout;
     mainLayout-&gt;addWidget(fileLabel, 0, 0);
     mainLayout-&gt;addWidget(fileComboBox, 0, 1, 1, 2);
     mainLayout-&gt;addWidget(textLabel, 1, 0);
     mainLayout-&gt;addWidget(textComboBox, 1, 1, 1, 2);
     mainLayout-&gt;addWidget(directoryLabel, 2, 0);
     mainLayout-&gt;addWidget(directoryComboBox, 2, 1);
     mainLayout-&gt;addWidget(browseButton, 2, 2);
     mainLayout-&gt;addWidget(filesTable, 3, 0, 1, 3);
     mainLayout-&gt;addWidget(filesFoundLabel, 4, 0, 1, 3);
     mainLayout-&gt;addLayout(buttonsLayout, 5, 0, 1, 3);
     setLayout(mainLayout);

     setWindowTitle(tr(&quot;Find Files&quot;));
     resize(700, 300);
 }

 void Window::browse()
 {
     QString directory = QFileDialog::getExistingDirectory(this,
                                tr(&quot;Find Files&quot;), QDir::currentPath());

     if (!directory.isEmpty()) {
         if (directoryComboBox-&gt;findText(directory) == -1)
             directoryComboBox-&gt;addItem(directory);
         directoryComboBox-&gt;setCurrentIndex(directoryComboBox-&gt;findText(directory));
     }
 }

 static void updateComboBox(QComboBox *comboBox)
 {
     if (comboBox-&gt;findText(comboBox-&gt;currentText()) == -1)
         comboBox-&gt;addItem(comboBox-&gt;currentText());
 }

 void Window::find()
 {
     filesTable-&gt;setRowCount(0);

     QString fileName = fileComboBox-&gt;currentText();
     QString text = textComboBox-&gt;currentText();
     QString path = directoryComboBox-&gt;currentText();

     updateComboBox(fileComboBox);
     updateComboBox(textComboBox);
     updateComboBox(directoryComboBox);

     currentDir = QDir(path);
     QStringList files;
     if (fileName.isEmpty())
         fileName = &quot;*&quot;;
     files = currentDir.entryList(QStringList(fileName),
                                  QDir::Files | QDir::NoSymLinks);

     if (!text.isEmpty())
         files = findFiles(files, text);
     showFiles(files);
 }

 QStringList Window::findFiles(const QStringList &amp;files, const QString &amp;text)
 {
     QProgressDialog progressDialog(this);
     progressDialog.setCancelButtonText(tr(&quot;&amp;Cancel&quot;));
     progressDialog.setRange(0, files.size());
     progressDialog.setWindowTitle(tr(&quot;Find Files&quot;));

     QStringList foundFiles;

     for (int i = 0; i &lt; files.size(); ++i) {
         progressDialog.setValue(i);
         progressDialog.setLabelText(tr(&quot;Searching file number %1 of %2...&quot;)
                                     .arg(i).arg(files.size()));
         qApp-&gt;processEvents();

         if (progressDialog.wasCanceled())
             break;

         QFile file(currentDir.absoluteFilePath(files[i]));

         if (file.open(QIODevice::ReadOnly)) {
             QString line;
             QTextStream in(&amp;file);
             while (!in.atEnd()) {
                 if (progressDialog.wasCanceled())
                     break;
                 line = in.readLine();
                 if (line.contains(text)) {
                     foundFiles &lt;&lt; files[i];
                     break;
                 }
             }
         }
     }
     return foundFiles;
 }

 void Window::showFiles(const QStringList &amp;files)
 {
     for (int i = 0; i &lt; files.size(); ++i) {
         QFile file(currentDir.absoluteFilePath(files[i]));
         qint64 size = QFileInfo(file).size();

         QTableWidgetItem *fileNameItem = new QTableWidgetItem(files[i]);
         fileNameItem-&gt;setFlags(fileNameItem-&gt;flags() ^ Qt::ItemIsEditable);
         QTableWidgetItem *sizeItem = new QTableWidgetItem(tr(&quot;%1 KB&quot;)
                                              .arg(int((size + 1023) / 1024)));
         sizeItem-&gt;setTextAlignment(Qt::AlignRight | Qt::AlignVCenter);
         sizeItem-&gt;setFlags(sizeItem-&gt;flags() ^ Qt::ItemIsEditable);

         int row = filesTable-&gt;rowCount();
         filesTable-&gt;insertRow(row);
         filesTable-&gt;setItem(row, 0, fileNameItem);
         filesTable-&gt;setItem(row, 1, sizeItem);
     }
     filesFoundLabel-&gt;setText(tr(&quot;%1 file(s) found&quot;).arg(files.size()) +
                              (&quot; (Double click on a file to open it)&quot;));
 }

 QPushButton *Window::createButton(const QString &amp;text, const char *member)
 {
     QPushButton *button = new QPushButton(text);
     connect(button, SIGNAL(clicked()), this, member);
     return button;
 }

 QComboBox *Window::createComboBox(const QString &amp;text)
 {
     QComboBox *comboBox = new QComboBox;
     comboBox-&gt;setEditable(true);
     comboBox-&gt;addItem(text);
     comboBox-&gt;setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
     return comboBox;
 }

 void Window::createFilesTable()
 {
     filesTable = new QTableWidget(0, 2);
     filesTable-&gt;setSelectionBehavior(QAbstractItemView::SelectRows);

     QStringList labels;
     labels &lt;&lt; tr(&quot;File Name&quot;) &lt;&lt; tr(&quot;Size&quot;);
     filesTable-&gt;setHorizontalHeaderLabels(labels);
     filesTable-&gt;horizontalHeader()-&gt;setResizeMode(0, QHeaderView::Stretch);
     filesTable-&gt;verticalHeader()-&gt;hide();
     filesTable-&gt;setShowGrid(false);

     connect(filesTable, SIGNAL(cellActivated(int,int)),
             this, SLOT(openFileOfItem(int,int)));
 }


 void Window::openFileOfItem(int row, int <span class="comment">/* column */</span>)
 {
     QTableWidgetItem *item = filesTable-&gt;item(row, 0);

     QDesktopServices::openUrl(QUrl::fromLocalFile(currentDir.absoluteFilePath(item-&gt;text())));
 }</pre>
<p /><address><hr /><div align="center">
<table width="100%" cellspacing="0" border="0"><tr class="address">
<td width="40%" align="left">Copyright &copy; 2010 Nokia Corporation and/or its subsidiary(-ies)</td>
<td width="20%" align="center"><a href="trademarks.html">Trademarks</a></td>
<td width="40%" align="right"><div align="right">Qt 4.6.3</div></td>
</tr></table></div></address></body>
</html>