-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilevalidator.cpp
More file actions
110 lines (91 loc) · 2.66 KB
/
filevalidator.cpp
File metadata and controls
110 lines (91 loc) · 2.66 KB
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
/*
Copyright 2020, Mitch Curtis
This file is part of Slate.
Slate 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.
Slate 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 Slate. If not, see <http://www.gnu.org/licenses/>.
*/
#include "filevalidator.h"
#include <QtGlobal>
#include <QFile>
#include <QImage>
FileValidator::FileValidator(QObject *parent) :
QObject(parent),
mTreatAsImage(false)
{
setFileErrorMessage("Must specify a file");
}
QUrl FileValidator::url() const
{
return mUrl;
}
void FileValidator::setUrl(const QUrl &url)
{
if (url == mUrl)
return;
mUrl = url;
if (mUrl.isEmpty()) {
setFileErrorMessage(tr("Must specify a file"));
} else if (!QFile::exists(mUrl.toLocalFile())) {
setFileErrorMessage(tr("File doesn't exist"));
} else {
if (mTreatAsImage) {
QImage image(mUrl.toLocalFile());
if (image.isNull()) {
setFileErrorMessage(tr("Image can not be opened"));
} else {
// The image was loaded successfully, so we can clear
// whatever was here before.
setFileErrorMessage(QString());
}
} else {
// The file was loaded successfully.
setFileErrorMessage(QString());
}
}
if (mFileErrorMessage.isEmpty()) {
// Let derived classes check for problems.
validate();
}
emit urlChanged();
}
bool FileValidator::isFileValid() const
{
return mFileErrorMessage.isEmpty();
}
QString FileValidator::fileErrorMessage() const
{
return mFileErrorMessage;
}
void FileValidator::setFileErrorMessage(const QString &fileErrorMessage)
{
if (fileErrorMessage == mFileErrorMessage)
return;
bool wasValid = isFileValid();
mFileErrorMessage = fileErrorMessage;
if (isFileValid() != wasValid) {
emit fileValidChanged();
}
emit fileErrorMessageChanged();
}
bool FileValidator::treatAsImage() const
{
return mTreatAsImage;
}
void FileValidator::setTreatAsImage(bool treatAsImage)
{
if (treatAsImage == mTreatAsImage)
return;
mTreatAsImage = treatAsImage;
emit treatAsImageChanged();
}
void FileValidator::validate()
{
}