1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2017 The Qt Company Ltd. |
4 | ** Contact: http://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the examples of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:BSD$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** BSD License Usage |
18 | ** Alternatively, you may use this file under the terms of the BSD license |
19 | ** as follows: |
20 | ** |
21 | ** "Redistribution and use in source and binary forms, with or without |
22 | ** modification, are permitted provided that the following conditions are |
23 | ** met: |
24 | ** * Redistributions of source code must retain the above copyright |
25 | ** notice, this list of conditions and the following disclaimer. |
26 | ** * Redistributions in binary form must reproduce the above copyright |
27 | ** notice, this list of conditions and the following disclaimer in |
28 | ** the documentation and/or other materials provided with the |
29 | ** distribution. |
30 | ** * Neither the name of The Qt Company Ltd nor the names of its |
31 | ** contributors may be used to endorse or promote products derived |
32 | ** from this software without specific prior written permission. |
33 | ** |
34 | ** |
35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
46 | ** |
47 | ** $QT_END_LICENSE$ |
48 | ** |
49 | ****************************************************************************/ |
50 | |
51 | #include "mainwindow.h" |
52 | #include "mimetypemodel.h" |
53 | |
54 | #include <QAction> |
55 | #include <QApplication> |
56 | #include <QFileDialog> |
57 | #include <QInputDialog> |
58 | #include <QMenu> |
59 | #include <QMenuBar> |
60 | #include <QMessageBox> |
61 | #include <QPlainTextEdit> |
62 | #include <QSplitter> |
63 | #include <QStatusBar> |
64 | #include <QTextEdit> |
65 | #include <QTreeView> |
66 | |
67 | #include <QFileInfo> |
68 | #include <QItemSelectionModel> |
69 | #include <QMimeDatabase> |
70 | #include <QMimeType> |
71 | |
72 | MainWindow::MainWindow(QWidget *parent) |
73 | : QMainWindow(parent) |
74 | , m_model(new MimetypeModel(this)) |
75 | , m_treeView(new QTreeView(this)) |
76 | , m_detailsText(new QTextEdit(this)) |
77 | , m_findIndex(0) |
78 | { |
79 | setWindowTitle(tr(s: "Qt Mime Database Browser" )); |
80 | |
81 | QMenu * = menuBar()->addMenu(title: tr(s: "&File" )); |
82 | QAction *detectFileAction = |
83 | fileMenu->addAction(text: tr(s: "&Detect File Type..." ), object: this, slot: &MainWindow::detectFile); |
84 | detectFileAction->setShortcuts(QKeySequence::Open); |
85 | |
86 | QAction *exitAction = fileMenu->addAction(text: tr(s: "E&xit" ), qApp, slot: &QApplication::closeAllWindows); |
87 | exitAction->setShortcuts(QKeySequence::Quit); |
88 | |
89 | QMenu * = menuBar()->addMenu(title: tr(s: "&Edit" )); |
90 | QAction *findAction = |
91 | findMenu->addAction(text: tr(s: "&Find..." ), object: this, slot: &MainWindow::find); |
92 | findAction->setShortcuts(QKeySequence::Find); |
93 | m_findNextAction = findMenu->addAction(text: tr(s: "Find &Next" ), object: this, slot: &MainWindow::findNext); |
94 | m_findNextAction->setShortcuts(QKeySequence::FindNext); |
95 | m_findPreviousAction = findMenu->addAction(text: tr(s: "Find &Previous" ), object: this, slot: &MainWindow::findPrevious); |
96 | m_findPreviousAction->setShortcuts(QKeySequence::FindPrevious); |
97 | |
98 | menuBar()->addMenu(title: tr(s: "&About" ))->addAction(text: tr(s: "&About Qt" ), qApp, slot: &QApplication::aboutQt); |
99 | |
100 | QSplitter *centralSplitter = new QSplitter(this); |
101 | setCentralWidget(centralSplitter); |
102 | m_treeView->setUniformRowHeights(true); |
103 | m_treeView->setModel(m_model); |
104 | |
105 | const auto items = m_model->findItems(text: "application/octet-stream" , flags: Qt::MatchContains | Qt::MatchFixedString | Qt::MatchRecursive); |
106 | if (!items.isEmpty()) |
107 | m_treeView->expand(index: m_model->indexFromItem(item: items.constFirst())); |
108 | |
109 | connect(sender: m_treeView->selectionModel(), signal: &QItemSelectionModel::currentChanged, |
110 | receiver: this, slot: &MainWindow::currentChanged); |
111 | centralSplitter->addWidget(widget: m_treeView); |
112 | m_detailsText->setReadOnly(true); |
113 | centralSplitter->addWidget(widget: m_detailsText); |
114 | |
115 | updateFindActions(); |
116 | } |
117 | |
118 | void MainWindow::currentChanged(const QModelIndex &index) |
119 | { |
120 | if (index.isValid()) |
121 | m_detailsText->setText(MimetypeModel::formatMimeTypeInfo(m_model->mimeType(index))); |
122 | else |
123 | m_detailsText->clear(); |
124 | } |
125 | |
126 | void MainWindow::selectAndGoTo(const QModelIndex &index) |
127 | { |
128 | m_treeView->scrollTo(index, hint: QAbstractItemView::PositionAtCenter); |
129 | m_treeView->setCurrentIndex(index); |
130 | } |
131 | |
132 | void MainWindow::detectFile() |
133 | { |
134 | const QString fileName = QFileDialog::getOpenFileName(parent: this, caption: tr(s: "Choose File" )); |
135 | if (fileName.isEmpty()) |
136 | return; |
137 | QMimeDatabase mimeDatabase; |
138 | const QFileInfo fi(fileName); |
139 | const QMimeType mimeType = mimeDatabase.mimeTypeForFile(fileInfo: fi); |
140 | const QModelIndex index = mimeType.isValid() |
141 | ? m_model->indexForMimeType(name: mimeType.name()) : QModelIndex(); |
142 | if (index.isValid()) { |
143 | statusBar()->showMessage(text: tr(s: "\"%1\" is of type \"%2\"" ).arg(args: fi.fileName(), args: mimeType.name())); |
144 | selectAndGoTo(index); |
145 | } else { |
146 | QMessageBox::information(parent: this, title: tr(s: "Unknown File Type" ), |
147 | text: tr(s: "The type of %1 could not be determined." ) |
148 | .arg(a: QDir::toNativeSeparators(pathName: fileName))); |
149 | } |
150 | } |
151 | |
152 | void MainWindow::updateFindActions() |
153 | { |
154 | const bool findNextPreviousEnabled = m_findMatches.size() > 1; |
155 | m_findNextAction->setEnabled(findNextPreviousEnabled); |
156 | m_findPreviousAction->setEnabled(findNextPreviousEnabled); |
157 | } |
158 | |
159 | void MainWindow::findPrevious() |
160 | { |
161 | if (--m_findIndex < 0) |
162 | m_findIndex = m_findMatches.size() - 1; |
163 | if (m_findIndex >= 0) |
164 | selectAndGoTo(index: m_findMatches.at(i: m_findIndex)); |
165 | } |
166 | |
167 | void MainWindow::findNext() |
168 | { |
169 | if (++m_findIndex >= m_findMatches.size()) |
170 | m_findIndex = 0; |
171 | if (m_findIndex < m_findMatches.size()) |
172 | selectAndGoTo(index: m_findMatches.at(i: m_findIndex)); |
173 | } |
174 | |
175 | void MainWindow::find() |
176 | { |
177 | QInputDialog inputDialog(this); |
178 | inputDialog.setWindowTitle(tr(s: "Find" )); |
179 | inputDialog.setLabelText(tr(s: "Text:" )); |
180 | if (inputDialog.exec() != QDialog::Accepted) |
181 | return; |
182 | const QString value = inputDialog.textValue().trimmed(); |
183 | if (value.isEmpty()) |
184 | return; |
185 | |
186 | m_findMatches.clear(); |
187 | m_findIndex = 0; |
188 | const QList<QStandardItem *> items = |
189 | m_model->findItems(text: value, flags: Qt::MatchContains | Qt::MatchFixedString | Qt::MatchRecursive); |
190 | for (const QStandardItem *item : items) |
191 | m_findMatches.append(t: m_model->indexFromItem(item)); |
192 | statusBar()->showMessage(text: tr(s: "%n mime types match \"%1\"." , c: 0, n: m_findMatches.size()).arg(a: value)); |
193 | updateFindActions(); |
194 | if (!m_findMatches.isEmpty()) |
195 | selectAndGoTo(index: m_findMatches.constFirst()); |
196 | } |
197 | |