-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
textnote.cpp
84 lines (70 loc) · 1.69 KB
/
textnote.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
#include "textnote.h"
#include "textedit.h"
#include <QTextStream>
#include <QClipboard>
#include <QApplication>
#include <QMessageBox>
TextNote::TextNote(const QFileInfo& fileinfo, Note::Type type_new)
: Note(fileinfo, type_new)
{
text_edit = new TextEdit();
load(); //loading note's content
connect(text_edit, SIGNAL(textChanged()), this, SLOT(contentChanged()));
text_edit->setAcceptRichText(false);
}
TextNote::~TextNote()
{
text_edit->deleteLater();
}
//Reading file
void TextNote::load()
{
file.close();
if(file.open(QIODevice::ReadOnly | QIODevice::Text))
{
QTextStream in(&file);
QString text = in.readAll();
text_edit->setPlainText(text);
file.close();
}
else if(file.open(QIODevice::WriteOnly | QIODevice::Text)) file.close(); //If file don't exist, we creating it
}
// Saving note
void TextNote::save(bool forced)
{
if (!(content_changed || forced)) {
return; //If file doesn't need in saving, exiting from function
}
file.close();
if(!file.open(QFile::WriteOnly | QFile::Text)) {
return;
}
QTextStream out(&file);
out << text_edit->toPlainText();
file.close();
content_changed = false;
}
//Returning widget (it's can be placed to tabwidget)
QWidget* TextNote::widget()
{
return text_edit;
}
bool TextNote::isDocumentSupported() const
{
return true;
}
QTextDocument* TextNote::document() const
{
return text_edit->document();
}
//Coping note's content to clipboard
void TextNote::copy() const
{
QClipboard* clipboard = QApplication::clipboard();
clipboard->setText(text_edit->toPlainText());
}
//Searching in a note's content
bool TextNote::find(const QString& text, bool from_start)
{
return text_edit->search(text, from_start);
}