void Document::loadPreferences(const Preferences& preferences) { m_always_center = preferences.alwaysCenter(); m_page_type = preferences.pageType(); switch (m_page_type) { case 1: m_page_amount = preferences.pageParagraphs(); break; case 2: m_page_amount = preferences.pageWords(); break; default: m_page_amount = preferences.pageCharacters(); break; } m_accurate_wordcount = preferences.accurateWordcount(); calculateWordCount(); m_block_cursor = preferences.blockCursor(); m_text->setCursorWidth(!m_block_cursor ? 1 : m_text->fontMetrics().averageCharWidth()); QFont font = m_text->font(); font.setStyleStrategy(preferences.smoothFonts() ? QFont::PreferAntialias : QFont::NoAntialias); m_text->setFont(font); m_highlighter->setEnabled(!isReadOnly() ? preferences.highlightMisspelled() : false); }
bool Document::loadFile(const QString& filename, int position) { bool loaded = true; if (filename.isEmpty()) { m_text->setReadOnly(false); scrollBarRangeChanged(m_scrollbar->minimum(), m_scrollbar->maximum()); calculateWordCount(); connect(m_text->document(), SIGNAL(contentsChange(int,int,int)), this, SLOT(updateWordCount(int,int,int))); connect(m_text->document(), SIGNAL(undoCommandAdded()), this, SLOT(undoCommandAdded())); return loaded; }
Document::Document(const QString& filename, int& current_wordcount, int& current_time, const QString& theme, QWidget* parent) : QWidget(parent), m_index(0), m_always_center(false), m_rich_text(false), m_cached_block_count(-1), m_cached_current_block(-1), m_page_type(0), m_page_amount(0), m_accurate_wordcount(true), m_current_wordcount(current_wordcount), m_current_time(current_time) { setMouseTracking(true); m_stats = &m_document_stats; m_hide_timer = new QTimer(this); m_hide_timer->setInterval(5000); m_hide_timer->setSingleShot(true); connect(m_hide_timer, SIGNAL(timeout()), this, SLOT(hideMouse())); // Set up text area m_text = new Editor(this); m_text->installEventFilter(this); m_text->setMouseTracking(true); m_text->setFrameStyle(QFrame::NoFrame); m_text->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); m_text->viewport()->setMouseTracking(true); m_text->viewport()->installEventFilter(this); QTextDocument* document = new QTextDocument(m_text); document->setUndoRedoEnabled(false); // Read file bool unknown_rich_text = false; if (!filename.isEmpty()) { m_rich_text = isRichTextFile(filename.toLower()); m_filename = QFileInfo(filename).canonicalFilePath(); updateState(); if (!m_rich_text) { QFile file(filename); if (file.open(QIODevice::ReadOnly | QIODevice::Text)) { QTextStream stream(&file); stream.setCodec(QTextCodec::codecForName("UTF-8")); stream.setAutoDetectUnicode(true); QTextCursor cursor(document); while (!stream.atEnd()) { cursor.insertText(stream.read(8192)); QApplication::processEvents(); } file.close(); } } else { PROSEUP::Reader reader; reader.read(filename, document); if (reader.hasError()) { QMessageBox::warning(this, tr("Sorry"), reader.errorString()); } } } // Set text area contents document->setUndoRedoEnabled(true); document->setModified(false); m_text->setDocument(document); m_text->setTabStopWidth(50); document->setIndentWidth(50); m_dictionary = new Dictionary(this); m_highlighter = new Highlighter(m_text, m_dictionary); connect(m_dictionary, SIGNAL(changed()), this, SLOT(dictionaryChanged())); if (m_filename.isEmpty()) { findIndex(); unknown_rich_text = true; } else { m_text->setReadOnly(!QFileInfo(m_filename).isWritable()); } // Set up scroll bar m_text->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); m_scrollbar = m_text->verticalScrollBar(); m_scrollbar->setPalette(QApplication::palette()); m_scrollbar->setAutoFillBackground(true); m_scrollbar->setVisible(false); connect(m_scrollbar, SIGNAL(actionTriggered(int)), this, SLOT(scrollBarActionTriggered(int))); connect(m_scrollbar, SIGNAL(rangeChanged(int,int)), this, SLOT(scrollBarRangeChanged(int,int))); scrollBarRangeChanged(m_scrollbar->minimum(), m_scrollbar->maximum()); // Lay out window m_layout = new QGridLayout(this); m_layout->setSpacing(0); m_layout->setMargin(0); m_layout->addWidget(m_text, 0, 1); m_layout->addWidget(m_scrollbar, 0, 2, Qt::AlignRight); // Load settings Preferences preferences; if (unknown_rich_text) { m_rich_text = preferences.richText(); } m_text->setAcceptRichText(m_rich_text); loadPreferences(preferences); loadTheme(theme); if(m_rich_text) { m_text->setUndoRedoEnabled(false); cleanUpDocument(); m_text->setUndoRedoEnabled(true); m_text->document()->setModified(false); } calculateWordCount(); connect(m_text->document(), SIGNAL(undoCommandAdded()), this, SLOT(undoCommandAdded())); connect(m_text->document(), SIGNAL(contentsChange(int,int,int)), this, SLOT(updateWordCount(int,int,int))); connect(m_text, SIGNAL(cursorPositionChanged()), this, SLOT(cursorPositionChanged())); connect(m_text, SIGNAL(selectionChanged()), this, SLOT(selectionChanged())); }
void Document::loadFile(const QString& filename, int position) { if (filename.isEmpty()) { return; } // Cache contents QFile::copy(filename, g_cache_path + m_cache_filename); // Load text area contents QTextDocument* document = m_text->document(); m_text->blockSignals(true); document->blockSignals(true); document->setUndoRedoEnabled(false); if (!m_rich_text) { QFile file(filename); if (file.open(QIODevice::ReadOnly | QIODevice::Text)) { QTextStream stream(&file); stream.setCodec(QTextCodec::codecForName("UTF-8")); stream.setAutoDetectUnicode(true); QTextCursor cursor(document); while (!stream.atEnd()) { cursor.insertText(stream.read(8192)); QApplication::processEvents(); } file.close(); } } else { QFile file(filename); if (file.open(QIODevice::ReadOnly)) { RTF::Reader reader; reader.read(&file, document); file.close(); if (reader.hasError()) { QMessageBox::warning(this, tr("Sorry"), reader.errorString()); } } } document->setUndoRedoEnabled(true); document->setModified(false); document->blockSignals(false); m_text->blockSignals(false); // Restore cursor position scrollBarRangeChanged(m_scrollbar->minimum(), m_scrollbar->maximum()); QTextCursor cursor = m_text->textCursor(); if (position != -1) { cursor.setPosition(position); } else { cursor.movePosition(QTextCursor::End); } m_text->setTextCursor(cursor); centerCursor(true); // Update details m_cached_stats.clear(); calculateWordCount(); m_highlighter->rehighlight(); }