Projectkk2glider/issue 4086 usb and serial (#4097)
[opentx.git] / companion / src / customizesplashdialog.cpp
blob6aaa6c773ef8872be814abe2e911c3c20be59b62
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(QObject::tr("FW: %1").arg(fileName));
92 *saveToFileName = fileName;
93 *source = FW;
94 break;
95 case PICT:
96 fileNameEdit->setText(QObject::tr("Pict: %1").arg(fileName));
97 *saveToFileName = fileName;
98 *source = PICT;
99 break;
100 case PROFILE:
101 fileNameEdit->setText(QObject::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 Class ***
155 customizeSplashDialog::customizeSplashDialog(QWidget *parent) :
156 QDialog(parent),
157 ui(new Ui::customizeSplashDialog)
159 ui->setupUi(this);
160 setWindowIcon(CompanionIcon("paintbrush.png"));
161 ui->leftLibraryButton->setIcon(CompanionIcon("library.png"));
162 ui->rightLibraryButton->setIcon(CompanionIcon("library.png"));
164 left.imageLabel = ui->leftImageLabel;
165 right.imageLabel = ui->rightImageLabel;
166 left.fileNameEdit = ui->leftFileNameEdit;
167 right.fileNameEdit = ui->rightFileNameEdit;
168 left.saveButton = ui->leftSaveButton;
169 right.saveButton = ui->rightSaveButton;
170 left.libraryButton = ui->leftLibraryButton;
171 right.libraryButton = ui->rightLibraryButton;
172 left.invertButton = ui->leftInvertButton;
173 right.invertButton = ui->rightInvertButton;
175 left.loadFwButton = ui->leftLoadFwButton;
176 right.loadFwButton = ui->rightLoadFwButton;
177 left.loadPictButton = ui->leftLoadPictButton;
178 right.loadPictButton = ui->rightLoadPictButton;
179 left.loadProfileButton = ui->leftLoadProfileButton;
180 right.loadProfileButton = ui->rightLoadProfileButton;
182 loadProfile(left);
183 left.markSourceButton();
185 resize(0,0);
188 customizeSplashDialog::~customizeSplashDialog()
190 delete ui;
193 void customizeSplashDialog::on_copyRightToLeftButton_clicked() {
194 left.copyImage(right);
196 void customizeSplashDialog::on_copyLeftToRightButton_clicked() {
197 right.copyImage(left);
201 void customizeSplashDialog::on_leftLoadFwButton_clicked() {loadFirmware(left);}
202 void customizeSplashDialog::on_rightLoadFwButton_clicked() {loadFirmware(right);}
203 void customizeSplashDialog::loadFirmware(Side side)
205 QString fileName = QFileDialog::getOpenFileName(this, tr("Open Firmware File"), g.flashDir(), FLASH_FILES_FILTER);
206 if (!fileName.isEmpty()) {
207 if (!side.displayImage( fileName, FW ))
208 QMessageBox::critical(this, tr("Error"), tr("Can not load embedded image from firmware file %1.").arg(fileName));
209 else
210 g.flashDir( QFileInfo(fileName).dir().absolutePath() );
212 side.markSourceButton();
215 void customizeSplashDialog::on_leftLoadPictButton_clicked() {loadPicture(left);}
216 void customizeSplashDialog::on_rightLoadPictButton_clicked() {loadPicture(right);}
217 void customizeSplashDialog::loadPicture(Side side)
219 QString supportedImageFormats;
220 for (int formatIndex = 0; formatIndex < QImageReader::supportedImageFormats().count(); formatIndex++) {
221 supportedImageFormats += QLatin1String(" *.") + QImageReader::supportedImageFormats()[formatIndex];
223 QString fileName = QFileDialog::getOpenFileName(this,
224 tr("Open Image to load"), g.imagesDir(), tr("Images (%1)").arg(supportedImageFormats));
226 if (!fileName.isEmpty()) {
227 if (!side.displayImage( fileName, PICT ))
228 QMessageBox::critical(this, tr("Error"), tr("Cannot load the image file %1.").arg(fileName));
229 else
230 g.imagesDir( QFileInfo(fileName).dir().absolutePath() );
232 side.markSourceButton();
235 void customizeSplashDialog::on_leftLoadProfileButton_clicked() {loadProfile(left);}
236 void customizeSplashDialog::on_rightLoadProfileButton_clicked() {loadProfile(right);}
237 void customizeSplashDialog::loadProfile(Side side)
239 QString fileName=g.profile[g.id()].splashFile();
241 if (!fileName.isEmpty()) {
242 if (!side.displayImage( fileName, PROFILE ))
243 QMessageBox::critical(this, tr("Error"), tr("Cannot load profile image %1.").arg(fileName));
245 side.markSourceButton();
248 void customizeSplashDialog::on_leftLibraryButton_clicked(){libraryButton_clicked(left);}
249 void customizeSplashDialog::on_rightLibraryButton_clicked(){libraryButton_clicked(right);}
250 void customizeSplashDialog::libraryButton_clicked( Side side )
252 QString fileName;
253 SplashLibraryDialog *ld = new SplashLibraryDialog(this,&fileName);
254 ld->exec();
255 if (!fileName.isEmpty()) {
256 if (!side.displayImage( fileName, UNDEFINED ))
257 QMessageBox::critical(this, tr("Error"), tr("Cannot load the library image %1.").arg(fileName));
261 void customizeSplashDialog::on_leftSaveButton_clicked(){saveButton_clicked(left);}
262 void customizeSplashDialog::on_rightSaveButton_clicked(){saveButton_clicked(right);}
263 void customizeSplashDialog::saveButton_clicked( Side side )
265 if (side.saveImage()){
266 QMessageBox::information(this, tr("File Saved"), tr("The image was saved to the file %1").arg(*side.saveToFileName));
267 if ( !side.refreshImage()){
268 QMessageBox::critical(this, tr("Image Refresh Error"), tr("Failed to refresh image from file %1").arg(*side.saveToFileName));
271 else
272 QMessageBox::critical(this, tr("File Save Error"), tr("Failed to write image to %1").arg(*side.saveToFileName));
275 void customizeSplashDialog::on_leftInvertButton_clicked(){invertButton_clicked(left);}
276 void customizeSplashDialog::on_rightInvertButton_clicked(){invertButton_clicked(right);}
277 void customizeSplashDialog::invertButton_clicked(Side side)
279 QImage image = side.imageLabel->pixmap()->toImage();
280 image.invertPixels();
281 side.imageLabel->setPixmap(QPixmap::fromImage(image));