1 /***********************************************************************
2  *
3  * Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2017 Graeme Gott <graeme@gottcode.org>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  ***********************************************************************/
19 
20 #include "block_stats.h"
21 
22 #include "dictionary_ref.h"
23 #include "scene_model.h"
24 
25 //-----------------------------------------------------------------------------
26 
BlockStats(SceneModel * scene_model)27 BlockStats::BlockStats(SceneModel* scene_model) :
28 	m_characters(0),
29 	m_letters(0),
30 	m_spaces(0),
31 	m_words(0),
32 	m_scene(false),
33 	m_scene_model(scene_model)
34 {
35 }
36 
37 //-----------------------------------------------------------------------------
38 
~BlockStats()39 BlockStats::~BlockStats()
40 {
41 	if (m_scene) {
42 		Q_ASSERT(m_scene_model != 0);
43 		m_scene_model->removeScene(this);
44 	}
45 }
46 
47 //-----------------------------------------------------------------------------
48 
checkSpelling(const QString & text,const DictionaryRef & dictionary)49 void BlockStats::checkSpelling(const QString& text, const DictionaryRef& dictionary)
50 {
51 	m_misspelled.clear();
52 	if (!text.isEmpty()) {
53 		QStringRef word;
54 		while ((word = dictionary.check(text, word.position() + word.length())).isNull() == false) {
55 			m_misspelled.append(word);
56 		}
57 	}
58 	m_checked = Checked;
59 }
60 
61 //-----------------------------------------------------------------------------
62 
update(const QString & text)63 void BlockStats::update(const QString& text)
64 {
65 	m_checked = Unchecked;
66 	m_characters = text.length();
67 	m_letters = 0;
68 	m_spaces = 0;
69 	m_words = 0;
70 	bool word = false;
71 	QString::const_iterator end = text.constEnd();
72 	for (QString::const_iterator i = text.constBegin(); i != end; ++i) {
73 		if (i->isLetterOrNumber()) {
74 			if (word == false) {
75 				word = true;
76 				m_words++;
77 			}
78 			m_letters += (i->category() != QChar::Punctuation_Dash);
79 		} else if (i->isSpace()) {
80 			word = false;
81 			m_spaces++;
82 		} else if (*i != u'’' && *i != '\'' && *i != '-') {
83 			word = false;
84 		}
85 	}
86 }
87 
88 //-----------------------------------------------------------------------------
89