void	QsvTextOperationsWidget::on_replaceOldText_returnPressed()
{
	if (QApplication::keyboardModifiers().testFlag(Qt::ControlModifier) ||
	    QApplication::keyboardModifiers().testFlag(Qt::AltModifier) ||
	    QApplication::keyboardModifiers().testFlag(Qt::ShiftModifier) ) {
		on_replaceAll_clicked();
		showReplace();
		return;
	}

	QTextCursor c = m_searchCursor;
	QTextDocument *doc = getTextDocument();
	if (!doc){
		qDebug("%s:%d - no document found, using a wrong class? wrong parent?", __FILE__,__LINE__);
		return;
	}
	c = doc->find( replaceFormUi->findText->text(), c, getReplaceFlags() );
	if (c.isNull())
		return;

	int start = c.selectionStart();
	int end   = c.selectionEnd();
	c.beginEditBlock();
	c.deleteChar();
	c.insertText( replaceFormUi->replaceText->text() );
	c.setPosition(start,QTextCursor::KeepAnchor);
	c.setPosition(end  ,QTextCursor::MoveAnchor);
	c.endEditBlock();
	setTextCursor( c );

	// is there any other apperance of this text?
	m_searchCursor = c;
	updateReplaceInput();
}
Exemplo n.º 2
0
EditorWidgetQSci::EditorWidgetQSci(QWidget *parent) : EditorWidget(parent)
{
    m_widget = new QWidget(parent);
    QVBoxLayout *l = new QVBoxLayout(m_widget);

    m_editor = new QsciScintilla(m_widget);
    m_editor->setMarginLineNumbers(QsciScintilla::NumberMargin, true);
    m_editor->setMarginWidth(QsciScintilla::NumberMargin, "12322");
    m_editor->setBraceMatching(QsciScintilla::SloppyBraceMatch);
    m_editor->setAutoCompletionSource(QsciScintilla::AcsAll);
    m_editor->setAutoCompletionThreshold(3);
    m_editor->setAutoIndent(true);
    m_editor->setIndentationsUseTabs(false);
    m_editor->setIndentationWidth(4);
    m_editor->setUtf8(true);
    m_editor->setEolMode(QsciScintilla::EolUnix);

    m_search_widget = new QWidget(m_widget);
    m_search_ui = new Ui::QSciSearchBar;
    m_search_ui->setupUi(m_search_widget);

    l->addWidget(m_editor, 1);
    l->addWidget(m_search_widget);

    m_search_widget->setVisible(false);
    setSearchBarReplaceVisible(false);

    m_canUndo = false;
    m_canRedo = false;

    QShortcut *find_s = new QShortcut(QKeySequence("Ctrl+F"), m_widget);
    QShortcut *replace_s = new QShortcut(QKeySequence("Ctrl+R"), m_widget);
    QShortcut *find_esc_s = new QShortcut(QKeySequence("Esc"), m_widget);

    connect(m_editor, SIGNAL(modificationChanged(bool)), SLOT(modified(bool)));
    connect(m_editor, SIGNAL(textChanged()),             SLOT(checkUndoRedo()));
    connect(m_search_ui->expandBtn, SIGNAL(clicked(bool)), SLOT(setSearchBarReplaceVisible(bool)));
    connect(m_search_ui->closeBtn, SIGNAL(clicked()),    SLOT(hideSearch()));
    connect(find_esc_s, SIGNAL(activated()),             SLOT(hideSearch()));
    connect(find_s, SIGNAL(activated()),                 SLOT(showSearch()));
    connect(replace_s, SIGNAL(activated()),              SLOT(showReplace()));
    connect(m_search_ui->nextBtn, SIGNAL(clicked()), SLOT(findNext()));
    connect(m_search_ui->prevBtn, SIGNAL(clicked()), SLOT(findPrev()));
    connect(m_search_ui->findEdit, SIGNAL(textEdited(QString)), SLOT(findNext()));
    connect(m_search_ui->replaceBtn, SIGNAL(clicked()), SLOT(replace()));
    connect(m_search_ui->replaceAllBtn, SIGNAL(clicked()), SLOT(replaceAll()));
    connect(m_search_ui->findEdit, SIGNAL(returnPressed()), SLOT(findNext()));
}
void QsvTextOperationsWidget::initReplaceWidget()
{
	m_replace = new QWidget( (QWidget*) parent() );
	m_replace->setObjectName("m_replace");
	replaceFormUi = new Ui::replaceForm();
	replaceFormUi->setupUi(m_replace);
	replaceFormUi->optionsGroupBox->hide();
	replaceFormUi->findText->setFont( m_replace->parentWidget()->font() );
	replaceFormUi->replaceText->setFont( m_replace->parentWidget()->font() );
        if (replaceFormUi->frame->style()->inherits("QWindowsStyle"))
		replaceFormUi->frame->setFrameStyle(QFrame::StyledPanel);
	// otherwise it inherits the default font from the editor - fixed
	m_replace->setFont(QApplication::font());
	m_replace->adjustSize();
	m_replace->hide();

	connect(replaceFormUi->moreButton,SIGNAL(clicked()),this,SLOT(adjustBottomWidget()));
	connect(replaceFormUi->findText,SIGNAL(textChanged(QString)),this,SLOT(on_replaceText_modified(QString)));
	connect(replaceFormUi->replaceButton,SIGNAL(clicked()),this,SLOT(on_replaceOldText_returnPressed()));
	connect(replaceFormUi->closeButton,SIGNAL(clicked()),this, SLOT(showReplace()));
}
bool	 QsvTextOperationsWidget::eventFilter(QObject *obj, QEvent *event)
{
	if (obj != parent())
		return false;
	if (event->type() != QEvent::KeyPress)
		return false;

	QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
	switch (keyEvent->key()){
		case Qt::Key_Escape:
			if (m_search && m_search->isVisible()){
				showSearch();
				return true;
			} else if (m_replace && m_replace->isVisible()){
				showReplace();
				return true;
			}/* else if (m_gotoLine && m_gotoLine->isVisible()) {
				showGotoLine();
				return true;
			}*/
			break;
			
		case Qt::Key_Enter:
		case Qt::Key_Return:
			if (m_search && m_search->isVisible()){
				if (keyEvent->modifiers().testFlag(Qt::ControlModifier) ||
				    keyEvent->modifiers().testFlag(Qt::AltModifier) ||
				    keyEvent->modifiers().testFlag(Qt::ShiftModifier) )
					searchPrev();
				else
					searchNext();
				return true;
			} else if (m_replace && m_replace->isVisible()){
				if (keyEvent->modifiers().testFlag(Qt::ControlModifier) ||
				    keyEvent->modifiers().testFlag(Qt::AltModifier) ||
				    keyEvent->modifiers().testFlag(Qt::ShiftModifier) )
					on_replaceAll_clicked();
				else
					on_replaceOldText_returnPressed();
				return true;
			}
			
			// TODO replace, goto line
			break;

		case Qt::Key_Tab:
		case Qt::Key_Backtab:
			if (m_replace && m_replace->isVisible()){
				/*
				// TODO - no cycle yet.
				if (Qt::Key_Tab == keyEvent->key())
					m_replace->focusWidget()->nextInFocusChain()->setFocus();
				else
					m_replace->focusWidget()->previousInFocusChain()->setFocus();
				*/
				// Instead - cycle between those two input lines. IMHO good enough
				if (replaceFormUi->replaceText->hasFocus()){
					replaceFormUi->findText->setFocus();
					replaceFormUi->findText->selectAll();
				}
				else{
					replaceFormUi->replaceText->setFocus();
					replaceFormUi->replaceText->selectAll();
				}
				return true;
			}
			break;
	}

	return false;
}