-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileswidget.cpp
More file actions
67 lines (59 loc) · 2.15 KB
/
Copy pathfileswidget.cpp
File metadata and controls
67 lines (59 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "fileswidget.h"
#include <QVBoxLayout>
#include <QHeaderView>
#include <QStyleFactory>
#include <QStyleOptionViewItem>
#include <QPainter>
#include <QDir>
#include <QFileInfo>
#include <QStyledItemDelegate>
#include <QDebug>
class MusicHighlightDelegate : public QStyledItemDelegate
{
public:
explicit MusicHighlightDelegate(QObject *parent = nullptr) : QStyledItemDelegate(parent) {}
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
{
QStyleOptionViewItem opt = option;
QString filePath = index.data(QFileSystemModel::FilePathRole).toString();
if (QFileInfo(filePath).isFile()) {
QString suffix = QFileInfo(filePath).suffix().toLower();
if (suffix == "mp3" || suffix == "wav" || suffix == "flac" || suffix == "ogg" || suffix == "m4a") {
opt.palette.setColor(QPalette::Text, Qt::darkGreen);
opt.palette.setColor(QPalette::Highlight, QColor(144, 238, 144));
}
}
QStyledItemDelegate::paint(painter, opt, index);
}
};
FilesWidget::FilesWidget(QWidget *parent)
: QWidget(parent)
{
QVBoxLayout *layout = new QVBoxLayout(this);
m_treeView = new QTreeView(this);
m_model = new QFileSystemModel(this);
m_model->setRootPath(QDir::rootPath());
m_treeView->setModel(m_model);
m_treeView->setRootIndex(m_model->index(QDir::homePath()));
m_treeView->setHeaderHidden(false);
m_treeView->setAnimated(true);
m_treeView->setIndentation(20);
m_treeView->setSortingEnabled(true);
m_treeView->setItemDelegate(new MusicHighlightDelegate(this));
layout->addWidget(m_treeView);
connect(m_treeView, &QTreeView::doubleClicked, this, &FilesWidget::onDoubleClicked);
}
QString FilesWidget::currentSelectedFile() const
{
QModelIndex idx = m_treeView->currentIndex();
if (idx.isValid() && m_model->isDir(idx) == false)
return m_model->filePath(idx);
return QString();
}
void FilesWidget::onDoubleClicked(const QModelIndex &index)
{
if (!m_model->isDir(index)) {
QString path = m_model->filePath(index);
emit fileSelected(path);
}
}