Example #1
0
void Document::scrollBarRangeChanged(int, int max)
{
	m_scrollbar->blockSignals(true);
	m_scrollbar->setMaximum(max + m_text->viewport()->height());
	m_scrollbar->blockSignals(false);
	centerCursor();
}
Example #2
0
QString OCamlSource::stopDebugging( const QString &file, int start_char, int end_char , bool after)
{
    if ( curFile != file )
        loadFile( file );
    QTextCharFormat unselectedFormat;
    unselectedFormat.setBackground( Qt::white );

    QTextCursor cur = textCursor();
    cur.movePosition( QTextCursor::Start, QTextCursor::MoveAnchor );
    cur.movePosition( QTextCursor::End, QTextCursor::KeepAnchor );
    cur.mergeCharFormat( unselectedFormat );

    cur.movePosition( QTextCursor::Start, QTextCursor::MoveAnchor );
    cur.movePosition( QTextCursor::NextCharacter, QTextCursor::MoveAnchor, start_char );

    setTextCursor( cur );

    centerCursor();
    _start_char = start_char;
    _end_char = end_char;
    _after = after;
    timer_index = 0;
    markBreakPoints(false);
    markCurrentLocation();

    cur.movePosition( QTextCursor::Start, QTextCursor::MoveAnchor );
    cur.movePosition( QTextCursor::NextCharacter, QTextCursor::MoveAnchor, _start_char );
    cur.movePosition( QTextCursor::NextCharacter, QTextCursor::KeepAnchor, _end_char - _start_char );
    QString text = cur.selectedText();

    return text;
}
void TextZone::cursorPositionChangedSlot()
{
    if (QApplication::mouseButtons() == Qt::NoButton) {
        centerCursor();
    }


    emit cursorPositionChanged(this->textCursor().position());



    // for textstyle :
    QTextCursor tCursor = this->textCursor();

    if((tCursor.atStart() == true
        || tCursor.position() == 1
        || tCursor.position() == 0) && tCursor.hasSelection() == false){
        this->changeTextStyleSlot(textStyles->defaultStyleIndex());
    }

    int currentStyleIndex = textStyles->compareStylesWithText(tCursor.charFormat(), tCursor.blockFormat());

    emit setStyleSelectionSignal(currentStyleIndex);


}
Example #4
0
void Document::cursorPositionChanged()
{
    emit indentChanged(m_text->textCursor().blockFormat().indent());
    emit alignmentChanged();
    if (QApplication::mouseButtons() == Qt::NoButton) {
        centerCursor();
    }
}
Example #5
0
void QScriptEdit::gotoLine(int lineNumber)
{
    int blockNumber = lineNumber - m_baseLineNumber;
    const QTextBlock &block = document()->findBlockByNumber(blockNumber);
    if (block.isValid()) {
        setTextCursor(QTextCursor(block));
        centerCursor();
    }
}
Example #6
0
void CodeView::setLine(int line)
{
    const QTextBlock& block = document()->findBlockByNumber(line - 1);
    QTextCursor cursor(block);
    cursor.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, 0);
    setTextCursor(cursor);
    centerCursor();
    setFocus();
}
Example #7
0
void SourceCodeView::showSourceCode(const QString &absFileName, const int lineNum)
{
    QString fileText = fileHash.value(absFileName);

    if (fileText.isNull()) { // File not in hash
        m_currentFileName.clear();

        // Assume fileName is relative to directory
        QFile file(absFileName);

        if (!file.exists()) {
            clear();
            appendHtml(tr("<i>File %1 not available</i>").arg(absFileName));
            return;
        }
        if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
            clear();
            appendHtml(tr("<i>File %1 not readable</i>").arg(absFileName));
            return;
        }
        const QTextCodec *codec = QTextCodec::codecForName(m_codecName);
        const QByteArray contents = file.readAll();
        fileText = codec ? codec->toUnicode(contents) : QString::fromUtf8(contents);
        fileHash.insert(absFileName, fileText);
    }


    if (m_currentFileName != absFileName) {
        setPlainText(fileText);
        m_currentFileName = absFileName;
    }

    QTextCursor cursor = textCursor();
    cursor.setPosition(document()->findBlockByNumber(lineNum - 1).position());
    setTextCursor(cursor);
    centerCursor();
    cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);
    cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor);

    QTextEdit::ExtraSelection selectedLine;
    selectedLine.cursor = cursor;

    // Define custom color for line selection
    const QColor fg = palette().color(QPalette::Highlight);
    const QColor bg = palette().color(QPalette::Base);
    QColor col;
    const qreal ratio = 0.25;
    col.setRedF(fg.redF() * ratio + bg.redF() * (1 - ratio));
    col.setGreenF(fg.greenF() * ratio + bg.greenF() * (1 - ratio));
    col.setBlueF(fg.blueF() * ratio + bg.blueF() * (1 - ratio));

    selectedLine.format.setBackground(col);
    selectedLine.format.setProperty(QTextFormat::FullWidthSelection, true);
    setExtraSelections(QList<QTextEdit::ExtraSelection>() << selectedLine);
}
Example #8
0
void QScriptEdit::gotoLine(int lineNumber)
{
#ifndef QT_NO_SYNTAXHIGHLIGHTER
    int blockNumber = lineNumber - m_baseLineNumber;
    const QTextBlock &block = document()->findBlockByNumber(blockNumber);
    if (block.isValid()) {
        setTextCursor(QTextCursor(block));
        centerCursor();
    }
#else
    Q_UNUSED(lineNumber);
#endif
}
//--------------------------------------------------------------------------------
void TextZone::resizeEvent(QResizeEvent* event)
{
    centerCursor();
    textDocument->setTextWidth(this->width() - 20);// Fix ticket #10 : the wobbling of the text
    //    textDocument->setTextWidth(-1);

    QRect rect(this->mapToGlobal(this->pos()).x(), this->mapToGlobal(this->pos()).y(), this->width(), this->height());

    emit textZoneResized(rect);

    QWidget::resizeEvent(event);

}
Example #10
0
void TikzEditor::setCursorPosition(int row, int col)
{
	const int pos = getCursorPosition(row, col);
	QTextCursor cursor = textCursor();
	cursor.setPosition(pos, QTextCursor::MoveAnchor);
	setTextCursor(cursor);

	// make sure that the cursor is in the middle of the visible area
/*
	ensureCursorVisible();
	const int newPosition = verticalScrollBar()->value() + cursorRect().top() - viewport()->height() / 2;
	verticalScrollBar()->setValue(newPosition);
*/
	centerCursor();
	setFocus();
}
Example #11
0
void OCamlSource::nextTextSearch() 
{
    if ( !lineSearchArea->text().isEmpty() )
    {
        if ( toPlainText().contains( lineSearchArea->text() ) )
        {
            if ( !find( lineSearchArea->text(), QTextDocument::FindCaseSensitively ) )
            {
                QTextCursor cur = textCursor() ;
                cur.movePosition( QTextCursor::Start );
                setTextCursor( cur );
                find( lineSearchArea->text(), QTextDocument::FindCaseSensitively ) ;
            }
            centerCursor();
        }
    }
}
void TextZone::applyConfig()
{
    QSettings settings;
    settings.beginGroup( "Settings" );
    alwaysCenter = settings.value("TextArea/alwaysCenter", true).toBool();
    showScrollbar = settings.value("TextArea/showScrollbar", true).toBool();
    preventDoubleSpaceOption = settings.value("preventDoubleSpace", false).toBool();
    settings.endGroup();

    centerCursor();

    if(showScrollbar)
        setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    else
        setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);


    editWidget->applyConfig();
}
Example #13
0
void SyntaxTextEditor::gotoLine(int line, int column)
{
    const int blockNumber = line - 1;
    const QTextBlock &block = document()->findBlockByNumber(blockNumber);
    if (block.isValid()) {
        QTextCursor cursor(block);
        if (column > 0) {
            cursor.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, column);
        } else {
            int pos = cursor.position();
            while (document()->characterAt(pos).category() == QChar::Separator_Space) {
                ++pos;
            }
            cursor.setPosition(pos);
        }
        setTextCursor(cursor);
        centerCursor();
    }
}
Example #14
0
int _glfwPlatformCreateWindow(_GLFWwindow* window,
                              const _GLFWwndconfig* wndconfig,
                              const _GLFWctxconfig* ctxconfig,
                              const _GLFWfbconfig* fbconfig)
{
    if (!createNativeWindow(window, wndconfig))
        return GLFW_FALSE;

    if (ctxconfig->client != GLFW_NO_API)
    {
        if (ctxconfig->source == GLFW_NATIVE_CONTEXT_API)
        {
            if (!_glfwInitWGL())
                return GLFW_FALSE;
            if (!_glfwCreateContextWGL(window, ctxconfig, fbconfig))
                return GLFW_FALSE;
        }
        else
        {
            if (!_glfwInitEGL())
                return GLFW_FALSE;
            if (!_glfwCreateContextEGL(window, ctxconfig, fbconfig))
                return GLFW_FALSE;
        }
    }

    if (window->monitor)
    {
        _glfwPlatformShowWindow(window);
        _glfwPlatformFocusWindow(window);
        if (!acquireMonitor(window))
            return GLFW_FALSE;

        centerCursor(window);
    }

    return GLFW_TRUE;
}
Example #15
0
void _glfwPlatformSetCursorMode(_GLFWwindow* window, int mode)
{
    if (mode == GLFW_CURSOR_DISABLED)
    {
        _glfw.win32.disabledCursorWindow = window;
        _glfwPlatformGetCursorPos(window,
                                  &_glfw.win32.restoreCursorPosX,
                                  &_glfw.win32.restoreCursorPosY);
        centerCursor(window);
        updateClipRect(window);
    }
    else if (_glfw.win32.disabledCursorWindow == window)
    {
        _glfw.win32.disabledCursorWindow = NULL;
        updateClipRect(NULL);
        _glfwPlatformSetCursorPos(window,
                                  _glfw.win32.restoreCursorPosX,
                                  _glfw.win32.restoreCursorPosY);
    }

    if (cursorInClientArea(window))
        updateCursorImage(window);
}
Example #16
0
void Document::resizeEvent(QResizeEvent* event)
{
	centerCursor(true);
	QWidget::resizeEvent(event);
}
Example #17
0
void Document::loadTheme(const Theme& theme)
{
	m_text->document()->blockSignals(true);

        m_block_default_format.clear();
        for(int i=0;i<theme.definedDefaultFormatsForBlocks().length();i++)
            m_block_default_format.insert(theme.definedDefaultFormatsForBlocks()[i],theme.defaultFormatForBlock(theme.definedDefaultFormatsForBlocks()[i]));

	// Update colors
	QString contrast = (qGray(theme.textColor().rgb()) > 127) ? "black" : "white";
	QColor color = theme.foregroundColor();
	color.setAlpha(theme.foregroundOpacity() * 2.55f);
        m_text->setStyleSheet(
                QString("Editor { background: rgba(%1, %2, %3, %4); color: %5; selection-background-color: %6; selection-color: %7; padding: %8px; border-radius: %9px; }")
			.arg(color.red())
			.arg(color.green())
			.arg(color.blue())
			.arg(color.alpha())
			.arg(theme.textColor().name())
			.arg(theme.textColor().name())
			.arg(contrast)
			.arg(theme.foregroundPadding())
			.arg(theme.foregroundRounding())
	);
	if (m_highlighter->misspelledColor() != theme.misspelledColor()) {
		m_highlighter->setMisspelledColor(theme.misspelledColor());
	}

	// Update text
	QFont font = theme.textFont();
	font.setStyleStrategy(m_text->font().styleStrategy());
	if (m_text->font() != font) {
		m_text->setFont(font);
	}
	m_text->setCursorWidth(!m_block_cursor ? 1 : m_text->fontMetrics().averageCharWidth());

	int margin = theme.foregroundMargin();
	m_layout->setColumnMinimumWidth(0, margin);
	m_layout->setColumnMinimumWidth(2, margin);
	if (theme.foregroundPosition() < 3) {
		m_text->setFixedWidth(theme.foregroundWidth());

		switch (theme.foregroundPosition()) {
		case 0:
			m_layout->setColumnStretch(0, 0);
			m_layout->setColumnStretch(2, 1);
			break;

		case 2:
			m_layout->setColumnStretch(0, 1);
			m_layout->setColumnStretch(2, 0);
			break;

		case 1:
		default:
			m_layout->setColumnStretch(0, 1);
			m_layout->setColumnStretch(2, 1);
			break;
		};
	} else {
		m_text->setMinimumWidth(theme.foregroundWidth());
		m_text->setMaximumWidth(maximumSize().height());

		m_layout->setColumnStretch(0, 0);
		m_layout->setColumnStretch(2, 0);
	}
        cleanUpDocument(false);
	centerCursor(true);
	m_text->document()->blockSignals(false);
        m_highlighter->rehighlight();
}
Example #18
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()));
}
Example #19
0
void Document::loadTheme(const Theme& theme)
{
	m_text->document()->blockSignals(true);

	// Update colors
	QColor color = theme.foregroundColor();
	color.setAlpha(theme.foregroundOpacity() * 2.55f);
	m_text->setStyleSheet(
		QString("QTextEdit { background: rgba(%1, %2, %3, %4); color: %5; selection-background-color: %6; selection-color: %7; padding: %8px; }")
			.arg(color.red())
			.arg(color.green())
			.arg(color.blue())
			.arg(color.alpha())
			.arg(theme.textColor().name())
			.arg(theme.textColor().name())
			.arg(theme.foregroundColor().name())
			.arg(theme.foregroundPadding())
	);
	if (m_highlighter->misspelledColor() != theme.misspelledColor()) {
		m_highlighter->setMisspelledColor(theme.misspelledColor());
	}

	// Update text
	QFont font = theme.textFont();
	font.setStyleStrategy(m_text->font().styleStrategy());
	if (m_text->font() != font) {
		m_text->setFont(font);
	}
	m_text->setCursorWidth(!m_block_cursor ? 1 : m_text->fontMetrics().averageCharWidth());

	int margin = theme.foregroundMargin();
	m_layout->setColumnMinimumWidth(0, margin);
	m_layout->setColumnMinimumWidth(2, margin);
	if (theme.foregroundPosition() < 3) {
		m_text->setFixedWidth(theme.foregroundWidth());

		switch (theme.foregroundPosition()) {
		case 0:
			m_layout->setColumnStretch(0, 0);
			m_layout->setColumnStretch(2, 1);
			break;

		case 2:
			m_layout->setColumnStretch(0, 1);
			m_layout->setColumnStretch(2, 0);
			break;

		case 1:
		default:
			m_layout->setColumnStretch(0, 1);
			m_layout->setColumnStretch(2, 1);
			break;
		};
	} else {
		m_text->setMinimumWidth(theme.foregroundWidth());
		m_text->setMaximumWidth(maximumSize().height());

		m_layout->setColumnStretch(0, 0);
		m_layout->setColumnStretch(2, 0);
	}

	centerCursor(true);
	m_text->document()->blockSignals(false);
}
Example #20
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();
}