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
72MainWindow::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("Qt Mime Database Browser"));
80
81 QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
82 QAction *detectFileAction =
83 fileMenu->addAction(tr("&Detect File Type..."), this, &MainWindow::detectFile);
84 detectFileAction->setShortcuts(QKeySequence::Open);
85
86 QAction *exitAction = fileMenu->addAction(tr("E&xit"), qApp, &QApplication::closeAllWindows);
87 exitAction->setShortcuts(QKeySequence::Quit);
88
89 QMenu *findMenu = menuBar()->addMenu(tr("&Edit"));
90 QAction *findAction =
91 findMenu->addAction(tr("&Find..."), this, &MainWindow::find);
92 findAction->setShortcuts(QKeySequence::Find);
93 m_findNextAction = findMenu->addAction(tr("Find &Next"), this, &MainWindow::findNext);
94 m_findNextAction->setShortcuts(QKeySequence::FindNext);
95 m_findPreviousAction = findMenu->addAction(tr("Find &Previous"), this, &MainWindow::findPrevious);
96 m_findPreviousAction->setShortcuts(QKeySequence::FindPrevious);
97
98 menuBar()->addMenu(tr("&About"))->addAction(tr("&About Qt"), qApp, &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("application/octet-stream", Qt::MatchContains | Qt::MatchFixedString | Qt::MatchRecursive);
106 if (!items.isEmpty())
107 m_treeView->expand(m_model->indexFromItem(items.constFirst()));
108
109 connect(m_treeView->selectionModel(), &QItemSelectionModel::currentChanged,
110 this, &MainWindow::currentChanged);
111 centralSplitter->addWidget(m_treeView);
112 m_detailsText->setReadOnly(true);
113 centralSplitter->addWidget(m_detailsText);
114
115 updateFindActions();
116}
117
118void 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
126void MainWindow::selectAndGoTo(const QModelIndex &index)
127{
128 m_treeView->scrollTo(index, QAbstractItemView::PositionAtCenter);
129 m_treeView->setCurrentIndex(index);
130}
131
132void MainWindow::detectFile()
133{
134 const QString fileName = QFileDialog::getOpenFileName(this, tr("Choose File"));
135 if (fileName.isEmpty())
136 return;
137 QMimeDatabase mimeDatabase;
138 const QFileInfo fi(fileName);
139 const QMimeType mimeType = mimeDatabase.mimeTypeForFile(fi);
140 const QModelIndex index = mimeType.isValid()
141 ? m_model->indexForMimeType(mimeType.name()) : QModelIndex();
142 if (index.isValid()) {
143 statusBar()->showMessage(tr("\"%1\" is of type \"%2\"").arg(fi.fileName(), mimeType.name()));
144 selectAndGoTo(index);
145 } else {
146 QMessageBox::information(this, tr("Unknown File Type"),
147 tr("The type of %1 could not be determined.")
148 .arg(QDir::toNativeSeparators(fileName)));
149 }
150}
151
152void MainWindow::updateFindActions()
153{
154 const bool findNextPreviousEnabled = m_findMatches.size() > 1;
155 m_findNextAction->setEnabled(findNextPreviousEnabled);
156 m_findPreviousAction->setEnabled(findNextPreviousEnabled);
157}
158
159void MainWindow::findPrevious()
160{
161 if (--m_findIndex < 0)
162 m_findIndex = m_findMatches.size() - 1;
163 if (m_findIndex >= 0)
164 selectAndGoTo(m_findMatches.at(m_findIndex));
165}
166
167void MainWindow::findNext()
168{
169 if (++m_findIndex >= m_findMatches.size())
170 m_findIndex = 0;
171 if (m_findIndex < m_findMatches.size())
172 selectAndGoTo(m_findMatches.at(m_findIndex));
173}
174
175void MainWindow::find()
176{
177 QInputDialog inputDialog(this);
178 inputDialog.setWindowTitle(tr("Find"));
179 inputDialog.setLabelText(tr("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(value, Qt::MatchContains | Qt::MatchFixedString | Qt::MatchRecursive);
190 for (const QStandardItem *item : items)
191 m_findMatches.append(m_model->indexFromItem(item));
192 statusBar()->showMessage(tr("%n mime types match \"%1\".", 0, m_findMatches.size()).arg(value));
193 updateFindActions();
194 if (!m_findMatches.isEmpty())
195 selectAndGoTo(m_findMatches.constFirst());
196}
197