Exemplo n.º 1
0
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 QTextEdit(this);
	m_text->installEventFilter(this);
	m_text->setMouseTracking(true);
	m_text->setTabStopWidth(50);
	m_text->document()->setIndentWidth(50);
	m_text->setFrameStyle(QFrame::NoFrame);
	m_text->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
	m_text->viewport()->setMouseTracking(true);
	m_text->viewport()->installEventFilter(this);
	m_highlighter = new Highlighter(m_text);

	// Open 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);
				m_text->setUndoRedoEnabled(false);
				while (!stream.atEnd()) {
					m_text->insertPlainText(stream.read(8192));
					QApplication::processEvents();
				}
				m_text->setUndoRedoEnabled(true);
				file.close();
			}
		} else {
			RTF::Reader reader;
			reader.read(filename, m_text);
			if (reader.hasError()) {
				QMessageBox::warning(this, tr("Sorry"), reader.errorString());
			}
		}
		m_text->document()->setModified(false);
	}
	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);

	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(textChanged()), this, SLOT(centerCursor()));
	connect(m_text, SIGNAL(cursorPositionChanged()), this, SLOT(cursorPositionChanged()));
	connect(m_text, SIGNAL(selectionChanged()), this, SLOT(selectionChanged()));
}
Exemplo n.º 2
0
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();
}