QGCSettingsWidget.cc 4.98 KB
Newer Older
1 2
#include <QSettings>

lm's avatar
lm committed
3
#include "QGCSettingsWidget.h"
4
#include "MainWindow.h"
lm's avatar
lm committed
5 6
#include "ui_QGCSettingsWidget.h"

7 8 9
#include "LinkManager.h"
#include "MAVLinkProtocol.h"
#include "MAVLinkSettingsWidget.h"
10
#include "GAudioOutput.h"
11 12 13 14 15

//, Qt::WindowFlags flags

QGCSettingsWidget::QGCSettingsWidget(QWidget *parent, Qt::WindowFlags flags) :
    QDialog(parent, flags),
lm's avatar
lm committed
16 17 18
    ui(new Ui::QGCSettingsWidget)
{
    ui->setupUi(this);
19

20 21 22
	// Set the frame holding the options for the custom style frame to hidden by default
	ui->customStyleFrame->setVisible(false);

23 24
    // Add all protocols
    QList<ProtocolInterface*> protocols = LinkManager::instance()->getProtocols();
25
    foreach (ProtocolInterface* protocol, protocols) {
26
        MAVLinkProtocol* mavlink = dynamic_cast<MAVLinkProtocol*>(protocol);
27
        if (mavlink) {
28 29 30 31 32 33 34
            MAVLinkSettingsWidget* msettings = new MAVLinkSettingsWidget(mavlink, this);
            ui->tabWidget->addTab(msettings, "MAVLink");
        }
    }

    this->window()->setWindowTitle(tr("QGroundControl Settings"));

35 36 37 38 39
    // Audio preferences
    ui->audioMuteCheckBox->setChecked(GAudioOutput::instance()->isMuted());
    connect(ui->audioMuteCheckBox, SIGNAL(toggled(bool)), GAudioOutput::instance(), SLOT(mute(bool)));
    connect(GAudioOutput::instance(), SIGNAL(mutedChanged(bool)), ui->audioMuteCheckBox, SLOT(setChecked(bool)));

40 41 42 43
    // Reconnect
    ui->reconnectCheckBox->setChecked(MainWindow::instance()->autoReconnectEnabled());
    connect(ui->reconnectCheckBox, SIGNAL(clicked(bool)), MainWindow::instance(), SLOT(enableAutoReconnect(bool)));

44 45 46 47
    // Low power mode
    ui->lowPowerCheckBox->setChecked(MainWindow::instance()->lowPowerModeEnabled());
    connect(ui->lowPowerCheckBox, SIGNAL(clicked(bool)), MainWindow::instance(), SLOT(enableLowPowerMode(bool)));

48 49
    //Dock widget title bars
    ui->titleBarCheckBox->setChecked(MainWindow::instance()->dockWidgetTitleBarsEnabled());
50
    connect(ui->titleBarCheckBox,SIGNAL(clicked(bool)),MainWindow::instance(),SLOT(enableDockWidgetTitleBars(bool)));
51

52 53
    // Style
    MainWindow::QGC_MAINWINDOW_STYLE style = (MainWindow::QGC_MAINWINDOW_STYLE)MainWindow::instance()->getStyle();
54 55 56
    ui->styleChooser->setCurrentIndex(style);
	connect(ui->styleChooser, SIGNAL(currentIndexChanged(int)), this, SLOT(styleChanged(int)));
	connect(ui->customStyleFileButton, SIGNAL(clicked()), this, SLOT(selectStylesheet()));
57

58 59
    // Close / destroy
    connect(ui->buttonBox, SIGNAL(accepted()), this, SLOT(deleteLater()));
lm's avatar
lm committed
60 61 62 63 64 65
}

QGCSettingsWidget::~QGCSettingsWidget()
{
    delete ui;
}
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145

void QGCSettingsWidget::selectStylesheet()
{
    // Let user select style sheet. The root directory for the file picker is the user's home directory if they haven't loaded a custom style.
	// Otherwise it defaults to the directory of that custom file.
	QString findDir;
	if (MainWindow::instance()->getStyle() == MainWindow::QGC_MAINWINDOW_STYLE_CUSTOM_DARK || MainWindow::instance()->getStyle() == MainWindow::QGC_MAINWINDOW_STYLE_CUSTOM_LIGHT)
	{
		findDir = QDir::homePath();
	}
	else
	{
		findDir = MainWindow::instance()->getStyleSheet();
	}

	QString newStyleFileName = QFileDialog::getOpenFileName(this, tr("Specify stylesheet"), findDir, tr("CSS Stylesheet (*.css);;"));

    // Load the new style sheet if a valid one was selected.
    if (!newStyleFileName.isNull())
    {
        QFile styleSheet(newStyleFileName);
        if (styleSheet.exists())
        {
            if (!updateStyle())
			{
				QMessageBox msgBox;
				msgBox.setIcon(QMessageBox::Information);
				msgBox.setText(tr("QGroundControl did not load a new style"));
				msgBox.setInformativeText(tr("Stylesheet file %1 was not readable").arg(newStyleFileName));
				msgBox.setStandardButtons(QMessageBox::Ok);
				msgBox.setDefaultButton(QMessageBox::Ok);
				msgBox.exec();
			}
        }
		else
		{
			QMessageBox msgBox;
			msgBox.setIcon(QMessageBox::Information);
			msgBox.setText(tr("QGroundControl did not load a new style"));
			msgBox.setInformativeText(tr("Stylesheet file %1 was not readable").arg(newStyleFileName));
			msgBox.setStandardButtons(QMessageBox::Ok);
			msgBox.setDefaultButton(QMessageBox::Ok);
			msgBox.exec();
		}
    }
}

bool QGCSettingsWidget::updateStyle()
{
	switch (ui->styleChooser->currentIndex())
	{
	case 0:
		return MainWindow::instance()->loadStyle(MainWindow::QGC_MAINWINDOW_STYLE_DARK, QString());
	case 1:
		return MainWindow::instance()->loadStyle(MainWindow::QGC_MAINWINDOW_STYLE_LIGHT, QString());
	case 2:
		return MainWindow::instance()->loadStyle(MainWindow::QGC_MAINWINDOW_STYLE_CUSTOM_DARK, QString());
	case 3:
		return MainWindow::instance()->loadStyle(MainWindow::QGC_MAINWINDOW_STYLE_CUSTOM_LIGHT, QString());
	default:
		return false;
	}
}

void QGCSettingsWidget::styleChanged(int index)
{
	// If a custom style is selected, enable the advanced view for the custom stylesheet. Otherwise,
	// make sure it's hidden.
	if (index == 2 || index == 3)
	{
		ui->customStyleFrame->setVisible(true);
	}
	else
	{
		ui->customStyleFrame->setVisible(false);
	}
	
	// And trigger a style update.
	updateStyle();
}