1 #include "textnote.h"
2 #include "textedit.h"
3 
4 #include <QTextStream>
5 #include <QClipboard>
6 #include <QApplication>
7 #include <QMessageBox>
8 
TextNote(const QFileInfo & fileinfo,Note::Type type_new)9 TextNote::TextNote(const QFileInfo& fileinfo, Note::Type type_new)
10 	: Note(fileinfo, type_new)
11 {
12 	text_edit = new TextEdit();
13 
14 	load(); //loading note's content
15 
16 	connect(text_edit, SIGNAL(textChanged()), this, SLOT(contentChanged()));
17 
18 	text_edit->setAcceptRichText(false);
19 }
20 
~TextNote()21 TextNote::~TextNote()
22 {
23 	text_edit->deleteLater();
24 }
25 
26 //Reading file
load()27 void TextNote::load()
28 {
29 	file.close();
30 	if(file.open(QIODevice::ReadOnly | QIODevice::Text))
31 	{
32 		QTextStream in(&file);
33 		QString text = in.readAll();
34 		text_edit->setPlainText(text);
35 		file.close();
36 	}
37 	else if(file.open(QIODevice::WriteOnly | QIODevice::Text)) file.close(); //If file don't exist, we creating it
38 }
39 
40 // Saving note
save(bool forced)41 void TextNote::save(bool forced)
42 {
43     if (!(content_changed || forced)) {
44         return; //If file doesn't need in saving, exiting from function
45     }
46 	file.close();
47     if(!file.open(QFile::WriteOnly | QFile::Text)) {
48         return;
49     }
50 	QTextStream out(&file);
51 	out << text_edit->toPlainText();
52 	file.close();
53 	content_changed = false;
54 }
55 
56 //Returning widget (it's can be placed to tabwidget)
widget()57 QWidget* TextNote::widget()
58 {
59 	return text_edit;
60 }
61 
isDocumentSupported() const62 bool TextNote::isDocumentSupported() const
63 {
64     return true;
65 }
66 
document() const67 QTextDocument* TextNote::document() const
68 {
69     return text_edit->document();
70 }
71 
72 //Coping note's content to clipboard
copy() const73 void TextNote::copy() const
74 {
75 	QClipboard* clipboard = QApplication::clipboard();
76 	clipboard->setText(text_edit->toPlainText());
77 }
78 
79 //Searching in a note's content
find(const QString & text,bool from_start)80 bool TextNote::find(const QString& text, bool from_start)
81 {
82     return text_edit->search(text, from_start);
83 }
84 
85