예제 #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 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()));
}
예제 #2
0
Document::Document(const QString& filename, int& current_wordcount, int& current_time, QWidget* parent)
    : QWidget(parent),
      m_cache_filename(randomCacheFilename()),
      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->setFrameStyle(QFrame::NoFrame);
    m_text->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    m_text->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    m_text->setTabStopWidth(50);
    m_text->document()->setIndentWidth(50);
    m_text->horizontalScrollBar()->setAttribute(Qt::WA_NoMousePropagation);
    m_text->viewport()->setMouseTracking(true);
    m_text->viewport()->installEventFilter(this);
    connect(m_text, SIGNAL(cursorPositionChanged()), this, SLOT(cursorPositionChanged()));
    connect(m_text, SIGNAL(selectionChanged()), this, SLOT(selectionChanged()));
    connect(m_text->document(), SIGNAL(undoCommandAdded()), this, SLOT(undoCommandAdded()));
    connect(m_text->document(), SIGNAL(contentsChange(int,int,int)), this, SLOT(updateWordCount(int,int,int)));

    m_dictionary = new Dictionary(this);
    m_highlighter = new Highlighter(m_text, m_dictionary);
    m_focusmode = new FocusMode(m_text);
    connect(m_dictionary, SIGNAL(changed()), this, SLOT(dictionaryChanged()));

    // Set filename
    bool unknown_rich_text = false;
    if (!filename.isEmpty()) {
        m_rich_text = isRichTextFile(filename.toLower());
        m_filename = QFileInfo(filename).canonicalFilePath();
        updateState();
    }

    if (m_filename.isEmpty()) {
        findIndex();
        unknown_rich_text = true;
    } else {
        m_text->setReadOnly(!QFileInfo(m_filename).isWritable());
    }

    // Set up scroll bar
    m_scrollbar = m_text->verticalScrollBar();
    m_scrollbar->setAttribute(Qt::WA_NoMousePropagation);
    m_scrollbar->setPalette(QApplication::palette());
    m_scrollbar->setAutoFillBackground(true);
    m_scrollbar->setMouseTracking(true);
    m_scrollbar->installEventFilter(this);
    setScrollBarVisible(false);
    connect(m_scrollbar, SIGNAL(actionTriggered(int)), this, SLOT(scrollBarActionTriggered(int)));
    connect(m_scrollbar, SIGNAL(rangeChanged(int,int)), this, SLOT(scrollBarRangeChanged(int,int)));

    // 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);
}