Make TX volatge for simu more flexible (#7124)
[opentx.git] / companion / src / splashlibrarydialog.cpp
blob0d3d71fad47e098ae6c1cf35f0d24569947a9d51
1 /*
2 * Copyright (C) OpenTX
4 * Based on code named
5 * th9x - http://code.google.com/p/th9x
6 * er9x - http://code.google.com/p/er9x
7 * gruvin9x - http://code.google.com/p/gruvin9x
9 * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
21 #include "splashlibrarydialog.h"
22 #include "ui_splashlibrarydialog.h"
23 #include "appdata.h"
24 #include "helpers.h"
25 #include "firmwareinterface.h"
26 #include "helpers.h"
28 SplashLibraryDialog::SplashLibraryDialog(QWidget *parent, QString * filename):
29 QDialog(parent),
30 ui(new Ui::SplashLibraryDialog),
31 splashFileName(filename)
33 ui->setupUi(this);
34 setWindowIcon(CompanionIcon("library.png"));
35 ui->nextPage->setIcon(CompanionIcon("arrow-right.png"));
36 ui->prevPage->setIcon(CompanionIcon("arrow-left.png"));
37 page = 0;
38 getFileList();
39 if (imageList.size() > 20) {
40 ui->nextPage->setEnabled(true);
42 setupPage(page);
44 foreach(splashLabel *sl, findChildren<splashLabel *>()) {
45 connect(sl, SIGNAL(buttonPressed(int)), this, SLOT(onButtonPressed(int)));
49 SplashLibraryDialog::~SplashLibraryDialog()
51 delete ui;
54 void SplashLibraryDialog::setupPage(int page)
56 splashLabel * sl[] = { ui->FwImage_01, ui->FwImage_02, ui->FwImage_03, ui->FwImage_04 ,
57 ui->FwImage_05, ui->FwImage_06, ui->FwImage_07, ui->FwImage_08 ,
58 ui->FwImage_09, ui->FwImage_10, ui->FwImage_11, ui->FwImage_12 ,
59 ui->FwImage_13, ui->FwImage_14, ui->FwImage_15, ui->FwImage_16 ,
60 ui->FwImage_17, ui->FwImage_18, ui->FwImage_19, ui->FwImage_20 };
61 for(int i=0; i<20; i++) {
62 if ((i + 20 * page) < imageList.size()) {
63 QImage image(imageList.at(i + 20 * page));
64 if (!image.isNull()) {
65 sl[i]->setPixmap(makePixMap(image));
66 sl[i]->setEnabled(true);
67 sl[i]->setId((i + 20 * page));
68 sl[i]->setStyleSheet("border:1px solid; border-color:#999999;");
70 else {
71 sl[i]->clear();
72 sl[i]->setDisabled(true);
73 sl[i]->setStyleSheet("border:1px;");
74 sl[i]->setId(-1);
77 else {
78 sl[i]->clear();
79 sl[i]->setDisabled(true);
80 sl[i]->setStyleSheet("border:1px;");
81 sl[i]->setId(-1);
84 setWindowTitle(tr("Splash Library - page %1 of %2").arg(page + 1).arg(ceil((float) imageList.size() / 20.0)));
87 void SplashLibraryDialog::getFileList()
89 imageList.clear();
90 if (g.embedSplashes()) {
91 QDir myRes(":/images/library");
92 QStringList tmp = myRes.entryList();
93 for (int i = 0; i < tmp.size(); i++) {
94 QFileInfo fileInfo = tmp.at(i);
95 imageList.append(":/images/library/" + fileInfo.fileName());
98 QString libraryPath = g.libDir();
99 if (!libraryPath.isEmpty()) {
100 if (QDir(libraryPath).exists()) {
101 QStringList supportedImageFormats;
102 for (int formatIndex = 0; formatIndex < QImageReader::supportedImageFormats().count(); formatIndex++) {
103 supportedImageFormats << QLatin1String("*.") + QImageReader::supportedImageFormats()[formatIndex];
105 QDir myDir(libraryPath);
106 myDir.setNameFilters(supportedImageFormats);
107 QStringList tmp = myDir.entryList();
108 for (int i = 0; i < tmp.size(); i++) {
109 QFileInfo fileInfo = tmp.at(i);
110 QString filename = libraryPath + "/" + fileInfo.fileName();
111 QImage image(filename);
112 if (!image.isNull()) {
113 imageList.append(filename);
115 else {
116 QMessageBox::warning(this, CPN_STR_TTL_WARNING, tr("Invalid image in library %1").arg(filename));
121 if (imageList.size() == 0) {
122 QMessageBox::information(this, CPN_STR_TTL_INFO, tr("No valid image found in library, check your settings"));
123 QTimer::singleShot(0, this, SLOT(dclose()));
125 else {
126 imageList.sort();
130 void SplashLibraryDialog::dclose()
132 close();
135 void SplashLibraryDialog::onButtonPressed(int button)
137 splashLabel * myLabel = qobject_cast<splashLabel *>(sender());
139 foreach(splashLabel *sl, findChildren<splashLabel *>()) {
140 if (sl->isEnabled()) {
141 sl->setStyleSheet("border:1px solid; border-color:#999999;");
144 if (button == Qt::Key_Enter) {
145 int id = myLabel->getId();
146 splashFileName->clear();
147 splashFileName->append(imageList.at(id));
148 close();
150 myLabel->setStyleSheet("border:1px solid; border-color:#00ffff");
153 void SplashLibraryDialog::on_nextPage_clicked() {
154 page++;
155 if (page >= (imageList.size() / 20)) {
156 ui->nextPage->setDisabled(true);
158 ui->prevPage->setEnabled(true);
159 setupPage(page);
162 void SplashLibraryDialog::on_prevPage_clicked() {
163 page--;
164 if (page == 0) {
165 ui->prevPage->setDisabled(true);
167 if (imageList.size() > 20) {
168 ui->nextPage->setEnabled(true);
170 setupPage(page);