WSL/SLF GitLab Repository

MainPanel.cc 5.69 KB
Newer Older
Mathias Bavay's avatar
Mathias Bavay committed
1
//SPDX-License-Identifier: GPL-3.0-or-later
2
3
4
5
6
/*****************************************************************************/
/*  Copyright 2019 WSL Institute for Snow and Avalanche Research  SLF-DAVOS  */
/*****************************************************************************/
/* This file is part of INIshell.
   INIshell is free software: you can redistribute it and/or modify
7
   it under the terms of the GNU General Public License as published by
8
9
10
11
12
13
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   INIshell is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
   GNU General Public License for more details.
15

16
17
   You should have received a copy of the GNU General Public License
   along with INIshell.  If not, see <http://www.gnu.org/licenses/>.
18
19
*/

20
21
22
23
24
25
26
27
28
29
30
#include <src/gui/MainPanel.h>
#include <src/gui/SectionButton.h>
#include <src/gui/WorkflowPanel.h>
#include <src/main/colors.h>
#include <src/main/constants.h>
#include <src/main/inishell.h>
#include <src/main/os.h>
#include <src/main/settings.h>
#include <src/main/XMLReader.h>
#include <src/panels/dynamic_panels.h>
#include <src/panels/panels.h>
31

32
#include <QGroupBox>
33
34
#include <QHBoxLayout>
#include <QListWidget>
35
#include <QPushButton>
36
#include <QRegularExpression>
37
#include <QSplitter>
38
#include <QVBoxLayout>
39

40
41
42
#ifdef DEBUG
	#include <iostream>
#endif
43

44
45
46
47
48
/**
 * @class MainPanel
 * @brief Constructor for the main panel, i. e. the tab widget with scroll area children.
 * @param[in] parent The parent widget (the main window).
 */
49
50
MainPanel::MainPanel(QWidget *parent) : QWidget(parent)
{
51
	/* widget(s) on the left side and main tab bar */
52
53
	workflow_panel_ = new WorkflowPanel;
	workflow_stack_ = new QStackedWidget;
54
	section_tab_ = new SectionTab;
55
	workflow_stack_->addWidget(section_tab_);
56

57
	/* main layout */
58
	auto *main_layout( new QHBoxLayout );
59
60
61
62
63
	splitter_ = new QSplitter;
	splitter_->addWidget(workflow_panel_); //allow to resize with mouse
	splitter_->addWidget(workflow_stack_);
	setSplitterSizes();
	main_layout->addWidget(splitter_);
64
	this->setLayout(main_layout);
Michael Reisecker's avatar
Michael Reisecker committed
65

66
	displayInfo(); //startup info text
67
}
68

69
70
71
72
73
/**
 * @brief Display some info and the SLF logo on program start.
 */
void MainPanel::displayInfo()
{
74
	static const QString message(tr("<br> \
75
76
	    &nbsp;&nbsp;Welcome to <b>INIshell</b>, a dynamic graphical user interface builder to manage INI files.<br> \
	    &nbsp;&nbsp;Double-click an application to the left to begin.<br><br> \
77
78
		&nbsp;&nbsp;For help, click \"Help\" in the menu and visit <a href=\"https://gitlabext.wsl.ch/snow-models/inishell\">INIshell's project page</a>.<br> \
		&nbsp;&nbsp;There, you will also find the well-documented <a href=\"https://gitlabext.wsl.ch/snow-models/inishell/-/tree/master\">source code</a>.<br> \
79
80
	    &nbsp;&nbsp;If you don't know where to begin, press %1. \
	").arg(os::getHelpSequence()));
81

82
	auto *info_scroll( new QScrollArea );
83
84
85
	info_scroll->setWidgetResizable(true);

	/* SLF logo */
86
	QLabel *info_label( new QLabel );
87
	info_label->setAlignment(Qt::AlignBottom | Qt::AlignRight);
88
	info_label->setStyleSheet("QLabel {background-color: " + colors::getQColor("app_bg").name() + "}");
89
	const QPixmap logo(":/icons/slf_desaturated.svg");
90
91
92
	info_label->setPixmap(logo);

	/* info text */
93
	QLabel *info_text( new QLabel(info_label) );
94
95
96
97
98
99
100
101
102
	info_text->setTextFormat(Qt::RichText);
	info_text->setTextInteractionFlags(Qt::TextBrowserInteraction);
	info_text->setOpenExternalLinks(true);
	info_text->setText(message);
	info_scroll->setWidget(info_label);

	section_tab_->addTab(info_scroll, "Info");
}

103
104
105
106
107
108
109
110
111
112
113
/**
 * @brief Query the splitter's current sizes.
 * @return Current sizes of the splitter's panels.
 */
QList<int> MainPanel::getSplitterSizes() const
{
	return splitter_->sizes();
}

/**
 * @brief Set sizes of the panels the splitter manages.
Michael Reisecker's avatar
Michael Reisecker committed
114
 * @param[in] sizes The sizes the splitter should give to its panels.
115
116
 * @param[in] enforce_visible Show the workflow with default size if its size is saved
 * to be zero?
117
 */
118
void MainPanel::setSplitterSizes(QList<int> sizes, bool enforce_visible)
119
{
120
121
	splitter_->setStretchFactor(0, Cst::proportion_workflow_horizontal_percent);
	splitter_->setStretchFactor(1, 100 - Cst::proportion_workflow_horizontal_percent);
122
123
124
125
126
127
128
	if (!sizes.isEmpty()) {
		splitter_->setSizes(sizes);
		return;
	}
	bool workflow_success, main_success;
	const int width_workflow = getSetting("auto::sizes::splitter_workflow", "size").toInt(&workflow_success);
	const int width_mainpanel = getSetting("auto::sizes::splitter_mainpanel", "size").toInt(&main_success);
129
	if (workflow_success && main_success && workflow_success && !(enforce_visible && width_workflow < 20) )
130
		splitter_->setSizes(sizes << width_workflow << width_mainpanel);
131
132
133
	else //reset to default
		splitter_->setSizes(QList<int>( {Cst::width_inishell_default * Cst::proportion_workflow_horizontal_percent / 100,
			Cst::width_inishell_default * (100 - Cst::proportion_workflow_horizontal_percent) / 100} )); //has constraints anyway - set something big
134
135
}

136
137
138
139
140
141
142
143
144
145
146
/**
 * @brief Remove all GUI elements.
 * @details This function clears the main GUI itself, i. e. all panels read from XML, including
 * the workflow panels.
 */
void MainPanel::clearGuiElements()
{
	section_tab_->clear();
	workflow_panel_->clearXmlPanels();
}

147
148
149
/**
 * @brief Reset the whole GUI to default values.
 */
150
void MainPanel::clearGui(const bool &set_default)
151
{
152
153
	clearDynamicPanels<Replicator>(); //clear special panels (the ones that can produce INI keys)
	clearDynamicPanels<Selector>(); //cf. notes there
154
	const QList<Atomic *> panel_list( section_tab_->findChildren<Atomic *>() ); //clear all others
155
	for (auto &panel : panel_list)
156
		panel->clear(set_default);
157
}