-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EditTabWidget.cpp
114 lines (99 loc) · 3.16 KB
/
EditTabWidget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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
// This source code is part of QAbc, a minimal ABC music notation editor.
// QAbc is Copyright © 2021 Benoît Rouits <[email protected]>.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// SPDX-License-Identifier: GPL-3.0-or-later
#include "AbcApplication.h"
#include "EditTabWidget.h"
#include <QFileInfo>
#include <QDebug>
#include <QMessageBox>
EditTabWidget::EditTabWidget(QWidget* parent)
: QTabWidget(parent)
{
setTabsClosable(true);
connect(this, &QTabWidget::tabCloseRequested, this, &EditTabWidget::askRemoveTab);
connect(this, &QTabWidget::currentChanged, this, &EditTabWidget::onCurrentTabChanged);
}
EditTabWidget::~EditTabWidget()
{
for (int i = count() -1; i >= 0; i-- ) {
removeTab(i);
}
}
QList<EditWidget*> *EditTabWidget::editWidgetList()
{
return &editwidgetlist;
}
EditWidget *EditTabWidget::currentEditWidget()
{
return currenteditwidget;
}
int EditTabWidget::addTab(EditWidget *swidget)
{
QFileInfo info(*(swidget->fileName()));
int ret = QTabWidget::addTab(swidget, info.baseName());
setCurrentWidget(swidget);
connect(swidget->editVBoxLayout()->abcPlainTextEdit(), &QPlainTextEdit::modificationChanged,
this, &EditTabWidget::onCurrentTextModified);
qDebug() << "addTab: " << (*swidget->fileName());
editwidgetlist.append(swidget);
currenteditwidget = swidget;
return ret;
}
void EditTabWidget::removeTab(int index)
{
EditWidget *w = editwidgetlist.at(index);
w->editVBoxLayout()->cleanup();
editwidgetlist.removeAt(index);
QTabWidget::removeTab(index);
delete w;
}
void EditTabWidget::removeTabs()
{
for (int i = count() -1; i >= 0; i-- ) {
removeTab(i);
}
}
void EditTabWidget::askRemoveTab(int index)
{
AbcApplication* a = static_cast<AbcApplication*>(qApp);
AbcMainWindow* m = a->mainWindow();
EditWidget *w = qobject_cast<EditWidget*>(widget(index));
if (!w->editVBoxLayout()->abcPlainTextEdit()->isSaved() &&
(QMessageBox::StandardButton::No == QMessageBox::question(m, tr("Really close?"),
tr("Current score not saved!\nClose this score anyway?"))))
return;
removeTab(index);
}
void EditTabWidget::onCurrentTabChanged(int index)
{
if (index < 0)
return;
EditWidget* swidget = static_cast<EditWidget*>(currentWidget());
for (int i = 0; i < editwidgetlist.length(); i++) {
if (editwidgetlist.at(i) == swidget) {
currenteditwidget = editwidgetlist.at(i);
qDebug() << "currentTab: " << index << (*currenteditwidget->fileName()) << i;
break;
}
}
}
void EditTabWidget::onCurrentTextModified(bool modified)
{
int ci = currentIndex();
QString text = tabText(ci);
if (modified) {
if (!text.endsWith('*'))
setTabText(ci, text + "*");
} else {
if (text.endsWith("*")) {
text.chop(1);
setTabText(ci, text);
}
}
}