Validate RSSI instance to allow user defined RSSI source (issue #5842) (#5846)
[opentx.git] / companion / src / customizesplashdialog.cpp
blob59b5e1de4a6624894b12350918c921adec7b9f92
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 "customizesplashdialog.h"
22 #include "ui_customizesplashdialog.h"
23 #include "flashfirmwaredialog.h"
24 #include "appdata.h"
25 #include "helpers.h"
26 #include "firmwareinterface.h"
27 #include "splashlibrarydialog.h"
29 //*** Side Class ***
31 Side::Side(){
32 imageLabel = 0;
33 fileNameEdit = 0;
34 saveButton = 0;
35 loadFwButton=0;
36 loadPictButton = 0;
37 loadProfileButton = 0;
38 saveToFileName = new QString("");
39 source = new Source(UNDEFINED);
40 format = new LCDFormat(LCDTARANIS);
43 void Side::markSourceButton()
45 loadFwButton->setChecked(*source == FW ? true : false );
46 loadPictButton->setChecked(*source == PICT ? true : false );
47 loadProfileButton->setChecked(*source == PROFILE ? true : false );
50 void Side::copyImage( Side side )
52 if ((*source!=UNDEFINED) && (*side.source!=UNDEFINED))
53 imageLabel->setPixmap(*side.imageLabel->pixmap());
56 bool Side::displayImage(QString fileName, Source pictSource)
58 QImage image;
60 if (fileName.isEmpty())
61 return false;
63 // Determine which picture format to use
64 if (pictSource == FW) {
65 FirmwareInterface firmware(fileName);
66 if (!firmware.hasSplash())
67 return false;
68 else
69 image = firmware.getSplash();
70 *format = (firmware.getSplashWidth()==WIDTH_TARANIS ? LCDTARANIS : LCD9X);
72 else {
73 image.load(fileName);
74 if (pictSource== PICT)
75 *format = image.width()>WIDTH_9X ? LCDTARANIS : LCD9X;
76 else if (pictSource == PROFILE)
77 *format = (g.profile[g.id()].fwType().contains("x9")) ? LCDTARANIS : LCD9X;
80 if (image.isNull()) {
81 return false;
84 // Load image
85 QPixmap pixmap = makePixMap(image);
86 imageLabel->setPixmap(pixmap);
87 imageLabel->setFixedSize(pixmap.width()*2, pixmap.height()*2);
89 switch (pictSource) {
90 case FW:
91 fileNameEdit->setText(CustomizeSplashDialog::tr("FW: %1").arg(fileName));
92 *saveToFileName = fileName;
93 *source = FW;
94 break;
95 case PICT:
96 fileNameEdit->setText(CustomizeSplashDialog::tr("Pict: %1").arg(fileName));
97 *saveToFileName = fileName;
98 *source = PICT;
99 break;
100 case PROFILE:
101 fileNameEdit->setText(CustomizeSplashDialog::tr("Profile image"));
102 *saveToFileName = fileName;
103 *source = PROFILE;
104 break;
105 default:
106 break;
108 saveButton->setEnabled(true);
109 libraryButton->setEnabled(true);
110 invertButton->setEnabled(true);
111 return true;
114 bool Side::refreshImage()
116 return displayImage( *saveToFileName, *source );
119 bool Side::saveImage()
121 if (*source == FW )
123 FirmwareInterface firmware(*saveToFileName);
124 if (!firmware.hasSplash()) {
125 return false;
127 QImage image = imageLabel->pixmap()->toImage().scaled(firmware.getSplashWidth(), firmware.getSplashHeight());
128 if (firmware.setSplash(image) && (firmware.save(*saveToFileName) > 0)) {
129 g.flashDir( QFileInfo(*saveToFileName).dir().absolutePath() );
131 else {
132 return false;
135 else if (*source == PICT) {
136 QImage image = imageLabel->pixmap()->toImage().scaled(imageLabel->width()/2, imageLabel->height()/2).convertToFormat(QImage::Format_Indexed8);
137 if (image.save(*saveToFileName)) {
138 g.imagesDir( QFileInfo(*saveToFileName).dir().absolutePath() );
140 else {
141 return false;
144 else if (*source == PROFILE) {
145 QImage image = imageLabel->pixmap()->toImage().scaled(imageLabel->width()/2, imageLabel->height()/2).convertToFormat(QImage::Format_Indexed8);
146 if (!image.save(*saveToFileName)) {
147 return false;
150 return true;
153 CustomizeSplashDialog::CustomizeSplashDialog(QWidget *parent) :
154 QDialog(parent),
155 ui(new Ui::CustomizeSplashDialog)
157 ui->setupUi(this);
158 setWindowIcon(CompanionIcon("paintbrush.png"));
159 ui->leftLibraryButton->setIcon(CompanionIcon("library.png"));
160 ui->rightLibraryButton->setIcon(CompanionIcon("library.png"));
162 left.imageLabel = ui->leftImageLabel;
163 right.imageLabel = ui->rightImageLabel;
164 left.fileNameEdit = ui->leftFileNameEdit;
165 right.fileNameEdit = ui->rightFileNameEdit;
166 left.saveButton = ui->leftSaveButton;
167 right.saveButton = ui->rightSaveButton;
168 left.libraryButton = ui->leftLibraryButton;
169 right.libraryButton = ui->rightLibraryButton;
170 left.invertButton = ui->leftInvertButton;
171 right.invertButton = ui->rightInvertButton;
173 left.loadFwButton = ui->leftLoadFwButton;
174 right.loadFwButton = ui->rightLoadFwButton;
175 left.loadPictButton = ui->leftLoadPictButton;
176 right.loadPictButton = ui->rightLoadPictButton;
177 left.loadProfileButton = ui->leftLoadProfileButton;
178 right.loadProfileButton = ui->rightLoadProfileButton;
180 loadProfile(left);
181 left.markSourceButton();
183 resize(0,0);
186 CustomizeSplashDialog::~CustomizeSplashDialog()
188 delete ui;
191 void CustomizeSplashDialog::on_copyRightToLeftButton_clicked() {
192 left.copyImage(right);
194 void CustomizeSplashDialog::on_copyLeftToRightButton_clicked() {
195 right.copyImage(left);
199 void CustomizeSplashDialog::on_leftLoadFwButton_clicked() {loadFirmware(left);}
200 void CustomizeSplashDialog::on_rightLoadFwButton_clicked() {loadFirmware(right);}
201 void CustomizeSplashDialog::loadFirmware(Side side)
203 QString fileName = QFileDialog::getOpenFileName(this, tr("Open Firmware File"), g.flashDir(), FLASH_FILES_FILTER);
204 if (!fileName.isEmpty()) {
205 if (!side.displayImage( fileName, FW ))
206 QMessageBox::critical(this, CPN_STR_TTL_ERROR, tr("Can not load embedded image from firmware file %1.").arg(fileName));
207 else
208 g.flashDir( QFileInfo(fileName).dir().absolutePath() );
210 side.markSourceButton();
213 void CustomizeSplashDialog::on_leftLoadPictButton_clicked() {loadPicture(left);}
214 void CustomizeSplashDialog::on_rightLoadPictButton_clicked() {loadPicture(right);}
215 void CustomizeSplashDialog::loadPicture(Side side)
217 QString supportedImageFormats;
218 for (int formatIndex = 0; formatIndex < QImageReader::supportedImageFormats().count(); formatIndex++) {
219 supportedImageFormats += QLatin1String(" *.") + QImageReader::supportedImageFormats()[formatIndex];
221 QString fileName = QFileDialog::getOpenFileName(this,
222 tr("Open Image to load"), g.imagesDir(), tr("Images (%1)").arg(supportedImageFormats));
224 if (!fileName.isEmpty()) {
225 if (!side.displayImage( fileName, PICT ))
226 QMessageBox::critical(this, CPN_STR_TTL_ERROR, tr("Cannot load the image file %1.").arg(fileName));
227 else
228 g.imagesDir( QFileInfo(fileName).dir().absolutePath() );
230 side.markSourceButton();
233 void CustomizeSplashDialog::on_leftLoadProfileButton_clicked() {loadProfile(left);}
234 void CustomizeSplashDialog::on_rightLoadProfileButton_clicked() {loadProfile(right);}
235 void CustomizeSplashDialog::loadProfile(Side side)
237 QString fileName=g.profile[g.id()].splashFile();
239 if (!fileName.isEmpty()) {
240 if (!side.displayImage( fileName, PROFILE ))
241 QMessageBox::critical(this, CPN_STR_TTL_ERROR, tr("Cannot load profile image %1.").arg(fileName));
243 side.markSourceButton();
246 void CustomizeSplashDialog::on_leftLibraryButton_clicked(){libraryButton_clicked(left);}
247 void CustomizeSplashDialog::on_rightLibraryButton_clicked(){libraryButton_clicked(right);}
248 void CustomizeSplashDialog::libraryButton_clicked( Side side )
250 QString fileName;
251 SplashLibraryDialog *ld = new SplashLibraryDialog(this,&fileName);
252 ld->exec();
253 if (!fileName.isEmpty()) {
254 if (!side.displayImage( fileName, UNDEFINED ))
255 QMessageBox::critical(this, CPN_STR_TTL_ERROR, tr("Cannot load the library image %1.").arg(fileName));
259 void CustomizeSplashDialog::on_leftSaveButton_clicked(){saveButton_clicked(left);}
260 void CustomizeSplashDialog::on_rightSaveButton_clicked(){saveButton_clicked(right);}
261 void CustomizeSplashDialog::saveButton_clicked( Side side )
263 if (side.saveImage()){
264 QMessageBox::information(this, tr("File Saved"), tr("The image was saved to the file %1").arg(*side.saveToFileName));
265 if ( !side.refreshImage()){
266 QMessageBox::critical(this, tr("Image Refresh Error"), tr("Failed to refresh image from file %1").arg(*side.saveToFileName));
269 else
270 QMessageBox::critical(this, tr("File Save Error"), tr("Failed to write image to %1").arg(*side.saveToFileName));
273 void CustomizeSplashDialog::on_leftInvertButton_clicked(){invertButton_clicked(left);}
274 void CustomizeSplashDialog::on_rightInvertButton_clicked(){invertButton_clicked(right);}
275 void CustomizeSplashDialog::invertButton_clicked(Side side)
277 QImage image = side.imageLabel->pixmap()->toImage();
278 image.invertPixels();
279 side.imageLabel->setPixmap(QPixmap::fromImage(image));