Repository navigation
Expand file tree
/
Copy pathsheetselectwidget.cpp
More file actions
81 lines (68 loc) · 2.33 KB
/
Copy pathsheetselectwidget.cpp
File metadata and controls
81 lines (68 loc) · 2.33 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include "sheetselectwidget.h"
#include <QDir>
#include <QGridLayout>
#include <QPushButton>
#include <QSettings>
SheetSelectWidget::SheetSelectWidget(QWidget* parent) :
QWidget(parent)
{
this->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
QGridLayout* layout = new QGridLayout();
this->setLayout(layout);
QSettings settings;
#ifdef Q_OS_ANDROID
QDir sheetsDir = settings.value("sheetsDir", "/sdcard/sheetMusic").toString();
#else
QDir sheetsDir = settings.value("sheetsDir", QDir::home().filePath(".sheetMusic")).toString();
#endif
QStringList pdfFilters;
pdfFilters << "*.pdf";
if (sheetsDir.exists())
{
pdfFiles = sheetsDir.entryInfoList(pdfFilters);
}
if (pdfFiles.count() > 0)
{
twoColumns = (pdfFiles.count() > 15);
rowsCount = (twoColumns ? (pdfFiles.count() + 1) / 2 : pdfFiles.count());
for (int i = 0; i < pdfFiles.count(); ++i)
{
bool secondCol = (twoColumns && (i >= rowsCount));
QString baseName = pdfFiles.at(i).baseName();
QPushButton* docButton = new QPushButton(baseName, this);
docButton->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
layout->addWidget(docButton, secondCol ? i - rowsCount : i, secondCol ? 1 : 0);
buttons.insert(docButton, pdfFiles.at(i));
connect(docButton, &QPushButton::clicked, this, &SheetSelectWidget::triggerSheetSelected);
}
}
}
void SheetSelectWidget::resizeEvent(QResizeEvent* event)
{
int maxTextWidth = (twoColumns ? (width() / 2) : width()) * 3 / 4;
QFont font;
font.setPixelSize(50);
QFontMetrics fontMetrics(font);
for (int i = 0; i < pdfFiles.count(); ++i)
{
QString baseName = pdfFiles.at(i).baseName();
while (fontMetrics.boundingRect(baseName).width() > maxTextWidth)
{
font.setPixelSize(font.pixelSize() - 2);
fontMetrics = QFontMetrics(font);
}
}
for (QPushButton* docButton : buttons.keys())
{
docButton->setFont(font);
}
QWidget::resizeEvent(event);
}
void SheetSelectWidget::triggerSheetSelected()
{
QPushButton* senderPushButton = dynamic_cast<QPushButton*>(sender());
if (senderPushButton)
{
emit sheetSelected(buttons[dynamic_cast<QPushButton*>(sender())]);
}
}