-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
htmlnote.cpp
109 lines (91 loc) · 2.59 KB
/
htmlnote.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
#include "htmlnote.h"
#include "textedit.h"
#include <QTextStream>
#include <QClipboard>
#include <QApplication>
#include <QMessageBox>
HtmlNote::HtmlNote(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()));
connect(text_edit, SIGNAL(currentCharFormatChanged(const QTextCharFormat &)),
this, SLOT(currentCharFormatChanged(const QTextCharFormat &)));
text_edit->setAcceptRichText(!settings.getNotePastePlaintext());
connect(&settings, SIGNAL(NotePastePlaintextChanged()), this, SLOT(noteNotePastePlaintextChanged()));
}
HtmlNote::~HtmlNote()
{
text_edit->deleteLater();
}
//Reading file
void HtmlNote::load()
{
file.close();
if(file.open(QIODevice::ReadOnly | QIODevice::Text))
{
QTextStream in(&file);
QString text = in.readAll();
text_edit->setHtml(text);
file.close();
}
else if(file.open(QIODevice::WriteOnly | QIODevice::Text)) file.close(); //If file don't exist, we creating it
}
//Saving note
void HtmlNote::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->toHtml();
file.close();
content_changed = false;
}
//Returning widget (it's can be placed to tabwidget)
QWidget* HtmlNote::widget()
{
return text_edit;
}
//Coping note's content to clipboard
void HtmlNote::copy() const
{
QClipboard* clipboard = QApplication::clipboard();
clipboard->setText(text_edit->toHtml());
}
//Searching in a note's content
bool HtmlNote::find(const QString& text, bool from_start)
{
return text_edit->search(text, from_start);
}
//Sending signal about changing format of a text under cursor
void HtmlNote::currentCharFormatChanged(const QTextCharFormat& format)
{
emit formatChanged(format.font());
}
//Applying format to selected text
void HtmlNote::setSelFormat(const QTextCharFormat& format)
{
QTextCursor cursor = text_edit->textCursor();
if(!cursor.hasSelection()) cursor.select(QTextCursor::WordUnderCursor);
cursor.mergeCharFormat(format);
}
//Returning format of selected text
QTextCharFormat HtmlNote::getSelFormat() const
{
QTextCursor cursor = text_edit->textCursor();
return cursor.charFormat();
}
void HtmlNote::noteNotePastePlaintextChanged()
{
text_edit->setAcceptRichText(!settings.getNotePastePlaintext());
}
bool HtmlNote::isDocumentSupported() const
{
return true;
}
QTextDocument *HtmlNote::document() const
{
return text_edit->document();
}