Changeset View
Changeset View
Standalone View
Standalone View
src/mainwindow.cpp
Show All 12 Lines | 1 | /* Atelier KDE Printer Host for 3D Printing | |||
---|---|---|---|---|---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
14 | GNU General Public License for more details. | 14 | GNU General Public License for more details. | ||
15 | 15 | | |||
16 | You should have received a copy of the GNU General Public License | 16 | You should have received a copy of the GNU General Public License | ||
17 | along with this program. If not, see <http://www.gnu.org/licenses/>. | 17 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
18 | */ | 18 | */ | ||
19 | #include "mainwindow.h" | 19 | #include "mainwindow.h" | ||
20 | #include "ui_mainwindow.h" | 20 | #include "ui_mainwindow.h" | ||
21 | #include <dialogs/connectsettingsdialog.h> | | |||
22 | #include <dialogs/profilesdialog.h> | 21 | #include <dialogs/profilesdialog.h> | ||
23 | #include <KLocalizedString> | 22 | #include <KLocalizedString> | ||
24 | #include <KStandardAction> | 23 | #include <KStandardAction> | ||
25 | #include <KActionCollection> | 24 | #include <KActionCollection> | ||
26 | #include <KXMLGUIFactory> | 25 | #include <KXMLGUIFactory> | ||
27 | #include <memory> | 26 | #include <memory> | ||
28 | #include <QFileDialog> | 27 | #include <QFileDialog> | ||
29 | #include <QHBoxLayout> | 28 | #include <QHBoxLayout> | ||
Show All 9 Lines | 37 | MainWindow::MainWindow(QWidget *parent) : | |||
39 | KXmlGuiWindow(parent), | 38 | KXmlGuiWindow(parent), | ||
40 | ui(new Ui::MainWindow), | 39 | ui(new Ui::MainWindow), | ||
41 | m_currEditorView(nullptr), | 40 | m_currEditorView(nullptr), | ||
42 | m_instances(new QTabWidget(this)) | 41 | m_instances(new QTabWidget(this)) | ||
43 | { | 42 | { | ||
44 | ui->setupUi(this); | 43 | ui->setupUi(this); | ||
45 | initWidgets(); | 44 | initWidgets(); | ||
46 | setupActions(); | 45 | setupActions(); | ||
46 | connect(m_instances, &QTabWidget::tabCloseRequested, [this] (int index){ | ||||
tcanabrava: why you are capturing everything by reference if there's not everything there? capture [this]. | |||||
In this case [&] and [this] should capture the same "this" referance. rizzitello: In this case [&] and [this] should capture the same "this" referance. | |||||
47 | QWidget *tempWidget= m_instances->widget(index); | ||||
48 | delete tempWidget; | ||||
tcanabrava: that's weird. | |||||
Thats how you correctly quit a tab. calling QTabWidget::remove only removes the tab from the view it does not delete it. We want ours removed instances to be completely closed if removed. rizzitello: Thats how you correctly quit a tab. calling [[ https://doc.qt.io/qt-5/qtabwidget.html#removeTab… | |||||
49 | | ||||
50 | if(m_instances->count() == 1) { | ||||
51 | m_instances->setTabsClosable(false); | ||||
52 | m_instances->setMovable(false); | ||||
53 | } | ||||
54 | }); | ||||
55 | | ||||
47 | } | 56 | } | ||
48 | 57 | | |||
49 | MainWindow::~MainWindow() | 58 | MainWindow::~MainWindow() | ||
50 | { | 59 | { | ||
51 | delete ui; | 60 | delete ui; | ||
52 | } | 61 | } | ||
53 | 62 | | |||
54 | void MainWindow::initWidgets() | 63 | void MainWindow::initWidgets() | ||
55 | { | 64 | { | ||
56 | auto newInstanceWidget = new AtCoreInstanceWidget(); | | |||
57 | m_instances->addTab(newInstanceWidget, i18n("Connect your printer")); | | |||
58 | 65 | | |||
59 | setupLateralArea(); | 66 | setupLateralArea(); | ||
67 | newAtCoreInstance(); | ||||
60 | 68 | | |||
61 | // View: | 69 | // View: | ||
62 | // Sidebar, Sidevar Controls, Printer Tabs. | 70 | // Sidebar, Sidevar Controls, Printer Tabs. | ||
63 | // Sidevar Controls and Printer Tabs can be resized, Sidebar cant. | 71 | // Sidevar Controls and Printer Tabs can be resized, Sidebar cant. | ||
64 | auto *centralLayout = new QHBoxLayout(); | 72 | auto *centralLayout = new QHBoxLayout(); | ||
65 | auto splitter = new QSplitter(); | 73 | auto splitter = new QSplitter(); | ||
66 | splitter->addWidget(m_lateral.m_stack); | 74 | splitter->addWidget(m_lateral.m_stack); | ||
67 | splitter->addWidget(m_instances); | 75 | splitter->addWidget(m_instances); | ||
68 | centralLayout->addWidget(m_lateral.m_toolBar); | 76 | centralLayout->addWidget(m_lateral.m_toolBar); | ||
69 | centralLayout->addWidget(splitter); | 77 | centralLayout->addWidget(splitter); | ||
70 | ui->centralwidget->setLayout(centralLayout); | 78 | ui->centralwidget->setLayout(centralLayout); | ||
71 | } | 79 | } | ||
72 | 80 | void MainWindow::newAtCoreInstance() | |||
81 | { | ||||
82 | auto newInstanceWidget = new AtCoreInstanceWidget(); | ||||
83 | QString name = QString::number(m_instances->addTab(newInstanceWidget, i18n("Connect a printer"))); | ||||
84 | newInstanceWidget->setObjectName(name); | ||||
85 | connect(this, &MainWindow::profilesChanged, newInstanceWidget, &AtCoreInstanceWidget::updateProfileData); | ||||
86 | connect(newInstanceWidget, &AtCoreInstanceWidget::connectionChanged, this, &MainWindow::atCoreInstanceNameChange); | ||||
87 | | ||||
88 | if(m_instances->count() > 1) { | ||||
89 | m_instances->setTabsClosable(true); | ||||
90 | m_instances->setMovable(true); | ||||
91 | } | ||||
92 | } | ||||
73 | // Move to LateralArea. | 93 | // Move to LateralArea. | ||
74 | void MainWindow::setupLateralArea() | 94 | void MainWindow::setupLateralArea() | ||
75 | { | 95 | { | ||
76 | m_lateral.m_toolBar = new QWidget(); | 96 | m_lateral.m_toolBar = new QWidget(); | ||
77 | m_lateral.m_stack = new QStackedWidget(); | 97 | m_lateral.m_stack = new QStackedWidget(); | ||
78 | auto *buttonLayout = new QVBoxLayout(); | 98 | auto buttonLayout = new QVBoxLayout(); | ||
79 | 99 | | |||
80 | auto setupButton = [this, buttonLayout](const QString& key, const QString& text, const QIcon& icon, QWidget *w) { | 100 | auto setupButton = [this, buttonLayout](const QString& key, const QString& text, const QIcon& icon, QWidget *w) { | ||
81 | auto *btn = new QPushButton(m_lateral.m_toolBar); | 101 | auto *btn = new QPushButton(m_lateral.m_toolBar); | ||
82 | btn->setToolTip(text); | 102 | btn->setToolTip(text); | ||
83 | btn->setAutoExclusive(true); | 103 | btn->setAutoExclusive(true); | ||
84 | btn->setCheckable(true); | 104 | btn->setCheckable(true); | ||
85 | btn->setIcon(icon); | 105 | btn->setIcon(icon); | ||
86 | btn->setIconSize(QSize(64,64)); | 106 | btn->setIconSize(QSize(64,64)); | ||
Show All 25 Lines | |||||
112 | void MainWindow::setupActions() | 132 | void MainWindow::setupActions() | ||
113 | { | 133 | { | ||
114 | // Actions for the Toolbar | 134 | // Actions for the Toolbar | ||
115 | QAction *action; | 135 | QAction *action; | ||
116 | action = actionCollection()->addAction(QStringLiteral("open_gcode")); | 136 | action = actionCollection()->addAction(QStringLiteral("open_gcode")); | ||
117 | action->setText(i18n("&Open GCode")); | 137 | action->setText(i18n("&Open GCode")); | ||
118 | connect(action, &QAction::triggered, this, &MainWindow::openFile); | 138 | connect(action, &QAction::triggered, this, &MainWindow::openFile); | ||
119 | 139 | | |||
120 | action = actionCollection()->addAction(QStringLiteral("connect")); | 140 | action = actionCollection()->addAction(QStringLiteral("new_instance")); | ||
121 | action->setText(i18n("&Connect")); | 141 | action->setText(i18n("&New Connection")); | ||
122 | connect(action, &QAction::triggered, [ & ]{ | 142 | connect(action, &QAction::triggered, this, &MainWindow::newAtCoreInstance); | ||
123 | std::unique_ptr<ConnectSettingsDialog> csd(new ConnectSettingsDialog); | | |||
124 | connect(csd.get(), &ConnectSettingsDialog::startConnection, [this](const QString& port, const QMap<QString, QVariant>& data) { | | |||
125 | newConnection(port, data); | | |||
126 | }); | | |||
127 | csd->exec(); | | |||
128 | }); | | |||
129 | 143 | | |||
130 | action = actionCollection()->addAction(QStringLiteral("profiles")); | 144 | action = actionCollection()->addAction(QStringLiteral("profiles")); | ||
131 | action->setText(i18n("&Profiles")); | 145 | action->setText(i18n("&Profiles")); | ||
132 | connect(action, &QAction::triggered, [this] { | 146 | connect(action, &QAction::triggered, [this] { | ||
133 | std::unique_ptr<ProfilesDialog> pd(new ProfilesDialog); | 147 | std::unique_ptr<ProfilesDialog> pd(new ProfilesDialog); | ||
134 | pd->exec(); | 148 | pd->exec(); | ||
149 | emit(profilesChanged()); | ||||
135 | }); | 150 | }); | ||
136 | 151 | | |||
137 | #ifdef Q_OS_LINUX | 152 | #ifdef Q_OS_LINUX | ||
138 | //only set icons from theme on linux | 153 | //only set icons from theme on linux | ||
139 | actionCollection()->action(QStringLiteral("profiles"))->setIcon(QIcon::fromTheme("emblem-favorite")); | 154 | actionCollection()->action(QStringLiteral("profiles"))->setIcon(QIcon::fromTheme("emblem-favorite")); | ||
140 | #endif | 155 | #endif | ||
141 | //use style's standardIcon for the icons we can. | 156 | //use style's standardIcon for the icons we can. | ||
142 | actionCollection()->action(QStringLiteral("open_gcode"))->setIcon(style()->standardIcon(QStyle::SP_DirOpenIcon)); | 157 | actionCollection()->action(QStringLiteral("open_gcode"))->setIcon(style()->standardIcon(QStyle::SP_DirOpenIcon)); | ||
Show All 17 Lines | 169 | if (!fileName.isEmpty()) { | |||
160 | m_openFiles.append(fileName); | 175 | m_openFiles.append(fileName); | ||
161 | 176 | | |||
162 | for(int i=0; i < tabs; ++i){ | 177 | for(int i=0; i < tabs; ++i){ | ||
163 | auto instance = qobject_cast<AtCoreInstanceWidget*>(m_instances->widget(i)); | 178 | auto instance = qobject_cast<AtCoreInstanceWidget*>(m_instances->widget(i)); | ||
164 | instance->setOpenFiles(m_openFiles); | 179 | instance->setOpenFiles(m_openFiles); | ||
165 | } | 180 | } | ||
166 | } | 181 | } | ||
167 | } | 182 | } | ||
168 | void MainWindow::newConnection(const QString& port, const QMap<QString, QVariant>& profile) | 183 | | ||
184 | void MainWindow::atCoreInstanceNameChange(const QString &name) | ||||
169 | { | 185 | { | ||
170 | const int tabs = m_instances->count(); | 186 | m_instances->setTabText(sender()->objectName().toInt(),name); | ||
171 | if(tabs == 1){ | | |||
172 | auto instance = qobject_cast<AtCoreInstanceWidget*>(m_instances->currentWidget()); | | |||
173 | if(!instance->connected()){ | | |||
174 | instance->startConnection(port, profile); | | |||
175 | m_instances->setTabText(m_instances->currentIndex(), profile["name"].toString()); | | |||
176 | return; | | |||
177 | } | | |||
178 | } | | |||
179 | auto newInstanceWidget = new AtCoreInstanceWidget(); | | |||
180 | m_instances->addTab(newInstanceWidget, profile["name"].toString()); | | |||
181 | newInstanceWidget->startConnection(port, profile); | | |||
182 | } | 187 | } |
why you are capturing everything by reference if there's not everything there? capture [this].