-
Notifications
You must be signed in to change notification settings - Fork 0
/
document_widget.cpp
168 lines (127 loc) · 3.32 KB
/
document_widget.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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/**
* Copyright 2022 naracanto <https://naracanto.github.io>.
*
* This file is part of QTabelo <https://github.com/beletalabs/qtabelo>.
*
* QTabelo is an open source table editor written in C++ using the
* Qt framework.
*
* QTabelo 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.
*
* QTabelo 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
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QTabelo. If not, see <https://www.gnu.org/licenses/>.
*/
#include "document_widget.h"
#include <QApplication>
#include <QClipboard>
#include <QCloseEvent>
#include <QDebug>
#include <QFile>
#include <QInputDialog>
#include <QMessageBox>
#include <QWidget>
#include "rename_dialog.h"
DocumentWidget::DocumentWidget(QWidget *parent)
: TableDocument(parent)
, m_modified{false}
, m_url{QUrl()}
{
setAttribute(Qt::WA_DeleteOnClose);
}
void DocumentWidget::closeEvent(QCloseEvent *event)
{
saveSettings();
event->accept();
}
//
// Property: modified
//
bool DocumentWidget::isModified() const
{
return m_modified;
}
void DocumentWidget::setModified(const bool modified)
{
if (modified != m_modified) {
m_modified = modified;
emit modifiedChanged(modified);
}
}
void DocumentWidget::resetModified()
{
m_modified = false;
emit modifiedChanged(false);
}
void DocumentWidget::initModified()
{
emit modifiedChanged(m_modified);
}
//
// Property: url
//
QUrl DocumentWidget::url() const
{
return m_url;
}
void DocumentWidget::setUrl(const QUrl &url)
{
if (url != m_url) {
m_url = url;
emit urlChanged(url);
}
}
void DocumentWidget::resetUrl()
{
m_url = QUrl();
emit urlChanged(QUrl());
}
void DocumentWidget::initUrl()
{
emit urlChanged(m_url);
}
//
// Document
//
void DocumentWidget::documentCountChanged(const int count)
{
slotAddTab(count);
}
//
//
//
void DocumentWidget::copyPathToClipboard()
{
if (!m_url.isEmpty())
QApplication::clipboard()->setText(m_url.toDisplayString(QUrl::PreferLocalFile));
}
void DocumentWidget::copyFilenameToClipboard()
{
if (!m_url.fileName().isEmpty())
QApplication::clipboard()->setText(m_url.fileName());
}
void DocumentWidget::renameFilename()
{
if (!m_url.isLocalFile())
return;
bool ok = false;
const QString oldFileName = m_url.fileName();
const QString newFileName = RenameDialog::getFileName(this, tr("Rename file"), tr("New file name"), QLineEdit::Normal, oldFileName, &ok);
if (!ok || newFileName.isEmpty() || (newFileName == oldFileName))
return;
QUrl newUrl = m_url.adjusted(QUrl::RemoveFilename).toString() + newFileName;
if (!QFile::rename(m_url.toLocalFile(), newUrl.toLocalFile())) {
QString title = tr("File Already Exists");
QString text = tr("A file with the name <em>%1</em> already exists!").arg(newUrl.fileName());
QMessageBox::critical(this, title, text);
return;
}
setUrl(newUrl);
}