PDA

View Full Version : def find python qt4



tsic
May 22nd, 2008, 04:08 PM
Hello,
I have a menu done with QT4.In Edit/Find I want to call my fORM FIND_Form
1) How to do that
2)This Form contains
- a Line Edit
- two buttons: Find and Close
- two radio buttons
- 3 Check Boxes: "whole words only", "Case sensitiive","Start at beginning

I found the code with C++.But I couldn't translate it to python .
How to do it??
And Find Next Code??
Thanks for help

fifth
May 22nd, 2008, 04:45 PM
Hello,
I have a menu done with QT4.In Edit/Find I want to call my fORM FIND_Form
1) How to do that
2)This Form contains
- a Line Edit
- two buttons: Find and Close
- two radio buttons
- 3 Check Boxes: "whole words only", "Case sensitiive","Start at beginning

I found the code with C++.But I couldn't translate it to python .
How to do it??
And Find Next Code??
Thanks for help

To show the dialog you will need to do something like;



from yourSourceFile import FIND_form
...
def find(self):
find_dlg = FIND_form(self)
find_dlg.show()


But there are plenty of PyQt tutorials around which will show you how to do this. If you have C++ code which would relate to the main form you are using and the find methods, post it up and I'll have a go at translating it.

tsic
May 22nd, 2008, 05:44 PM
Thank you,
this is findwidget.cpp


/************************************************** *************************
* copyright : (C) 2003-2007 by Pascal Brachet *
* http://www.xm1math.net/texmaker/ *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
************************************************** *************************/

#include "findwidget.h"

FindWidget::FindWidget(QWidget* parent)
: QWidget( parent)
{
ui.setupUi(this);
connect(ui.findButton, SIGNAL( clicked() ), this, SLOT( doFind() ) );
connect(ui.closeButton, SIGNAL( clicked() ), this, SLOT( doHide() ) );
ui.findButton->setShortcut(Qt::Key_Return);
}

FindWidget::~FindWidget()
{

}

void FindWidget::doFind()
{
if ( !editor ) return;
if ( !editor->search( ui.comboFind->currentText(), ui.checkCase->isChecked(), ui.checkWords->isChecked(), ui.radioForward->isChecked(), !ui.checkBegin->isChecked() ) )
{
ui.checkBegin->setChecked( TRUE );
}
else ui.checkBegin->setChecked( FALSE );
editor->viewport()->repaint();
}

void FindWidget::doHide()
{
hide();
if ( editor )
{
editor->viewport()->repaint();
editor->setFocus();
}
}

void FindWidget::SetEditor(LatexEditor *ed)
{
editor=ed;
}





and this is findwidget.h



/************************************************** *************************
* copyright : (C) 2003-2007 by Pascal Brachet *
* http://www.xm1math.net/texmaker/ *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
************************************************** *************************/

#ifndef FINDWIDGET_H
#define FINDWIDGET_H

#include "ui_findwidget.h"
#include "latexeditor.h"

class FindWidget : public QWidget
{
Q_OBJECT

public:
FindWidget(QWidget* parent = 0);
~FindWidget();
Ui::FindWidget ui;
public slots:
virtual void doFind();
void SetEditor(LatexEditor *ed);
void doHide();

protected:
LatexEditor *editor;
};

#endif




2) for the cal of forms I used that before:

from findText import Ui_FindText

def findClicked(self):
find_dlg = Ui_FindText()
find_dlg.show()


but a message error box appear:
Ui_FindText object has no attribute "show"

fifth
May 22nd, 2008, 08:41 PM
The code below is a rough translation,I cant fully check it as I dont have the other source files (ie for LatexEditor etc).

Are you trying to convert/duplicate the LatexEditor in Python? If not,I would look around for simpler examples for Find/Replace dialogs in qt.



# ************************************************** *************************
# * copyright : (C) 2003-2007 by Pascal Brachet *
# * http://www.xm1math.net/texmaker/ *
# * *
# * This program is free software; you can redistribute it and/or modify *
# * it under the terms of the GNU General Public License as published by *
# * the Free Software Foundation; either version 2 of the License, or *
# * (at your option) any later version. *
# * *
# ************************************************** *************************

from PyQt4.QtCore import *
from PyQt4.QtGui import *
import latexeditor
import ui_findwidget as ui

class findWidget(QWidget, ui.Ui_FindWidget):

def __init__(self, parent = None):
super(findWidget, self).__init__(parent)

self.editor = LatexEditor()
self.setupUi(self)
self.connect(self.findButton, SIGNAL("clicked()"), self.doFind())
self.connect(self.closeButton, SIGNAL("clicked()"), self.doHide())
self.findButton.setShortcut(Qt.Key_Return)

def doFind(self):
if not self.editor:
return
if not self.editor.search(self.comboFind.currentText(), self.checkCase.isChecked(), self.checkWords.isChecked(), self.radioForward.isChecked(), not(self.checkBegin.isChecked())):
self.checkBegin.setChecked( True )
else:
self.checkBegin.setChecked(False)
self.editor.viewport().repaint()

def doHide(self):
self.hide()
if self.editor:
self.editor.viewport().repaint()
self.editor.setFocus()

def SetEditor(self, ed):
self.editor=ed

tsic
May 22nd, 2008, 09:51 PM
Thank you a lot but you didn't answer to my question of show()
I didn't remarque in the beginning the import latexteditor this is his code It's too long but I think there is just some function which are related to my Find_window. Please just check for these functions. If there is a simpler code I will be pleased.



/************************************************** *************************
* copyright : (C) 2003-2007 by Pascal Brachet *
* http://www.xm1math.net/texmaker/ *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
************************************************** *************************/

#include "latexeditor.h"
//#include "parenmatcher.h"
#include <QPainter>
#include <QTextLayout>
#include <QMetaProperty>
#include <QDebug>
#include <QAction>
#include <QMenu>
#include <QApplication>
#include <QMimeData>
#include <QClipboard>
#include <QPalette>
#include <QKeyEvent>
#include <QAbstractItemView>
#include <QApplication>
#include <QModelIndex>
#include <QAbstractItemModel>
#include <QScrollBar>

LatexEditor::LatexEditor(QWidget *parent,QFont & efont, QColor colMath, QColor colCommand, QColor colKeyword) : QTextEdit(parent),c(0)
{
QPalette p = palette();
p.setColor(QPalette::Inactive, QPalette::Highlight,p.color(QPalette::Active, QPalette::Highlight));
p.setColor(QPalette::Inactive, QPalette::HighlightedText,p.color(QPalette::Active , QPalette::HighlightedText));
setPalette(p);
setAcceptRichText(false);
setLineWidth(0);
setFrameShape(QFrame::NoFrame);
for (int i = 0; i < 3; ++i) UserBookmark[i]=0;
encoding="";
setFont(efont);
setTabStopWidth(fontMetrics().width(" "));
setTabChangesFocus(false);
highlighter = new LatexHighlighter(document());
highlighter->setColors(colMath,colCommand,colKeyword);
//c=0;
connect(this, SIGNAL(cursorPositionChanged()), viewport(), SLOT(update()));
// matcher = new ParenMatcher;
// connect(this, SIGNAL(cursorPositionChanged()), matcher, SLOT(matchFromSender()));
//grabShortcut(QKeySequence(Qt::SHIFT + Qt::Key_Tab), Qt::WidgetShortcut);
setFocus();
}
LatexEditor::~LatexEditor(){
//delete matcher;
}

// void LatexEditor::clearMarkerFormat(const QTextBlock &block, int markerId)
// {
// QTextLayout *layout = block.layout();
// QList<QTextLayout::FormatRange> formats = layout->additionalFormats();
// bool formatsChanged = false;
// for (int i = 0; i < formats.count(); ++i)
// if (formats.at(i).format.hasProperty(markerId)) {
// formats[i].format.clearBackground();
// formats[i].format.clearProperty(markerId);
// if (formats.at(i).format.properties().isEmpty()) {
// formats.removeAt(i);
// --i;
// }
// formatsChanged = true;
// }
//
// if (formatsChanged)
// layout->setAdditionalFormats(formats);
// }

void LatexEditor::paintEvent(QPaintEvent *event)
{
QRect rect = cursorRect();
rect.setX(0);
rect.setWidth(viewport()->width());
QPainter painter(viewport());
const QBrush brush(QColor("#ececec"));
painter.fillRect(rect, brush);
painter.end();
QTextEdit::paintEvent(event);
}

void LatexEditor::contextMenuEvent(QContextMenuEvent *e)
{
QMenu *menu=new QMenu(this);
QAction *a;
a = menu->addAction(tr("Undo"), this, SLOT(undo()));
a->setShortcut(Qt::CTRL+Qt::Key_Z);
a->setEnabled(document()->isUndoAvailable());
a = menu->addAction(tr("Redo") , this, SLOT(redo()));
a->setShortcut(Qt::CTRL+Qt::Key_Y);
a->setEnabled(document()->isRedoAvailable());
menu->addSeparator();
a = menu->addAction(tr("Cut"), this, SLOT(cut()));
a->setShortcut(Qt::CTRL+Qt::Key_X);
a->setEnabled(textCursor().hasSelection());
a = menu->addAction(tr("Copy"), this, SLOT(copy()));
a->setShortcut(Qt::CTRL+Qt::Key_C);
a->setEnabled(textCursor().hasSelection());
a = menu->addAction(tr("Paste") , this, SLOT(paste()));
a->setShortcut(Qt::CTRL+Qt::Key_P);
const QMimeData *md = QApplication::clipboard()->mimeData();
a->setEnabled(md && canInsertFromMimeData(md));
menu->addSeparator();
a = menu->addAction(tr("Select All"), this, SLOT(selectAll()));
a->setShortcut(Qt::CTRL+Qt::Key_A);
a->setEnabled(!document()->isEmpty());
menu->addSeparator();
a = menu->addAction(tr("Check Spelling Word"), this, SLOT(checkSpellingWord()));
a->setEnabled(!document()->isEmpty());
a = menu->addAction(tr("Check Spelling Selection"), this, SLOT(checkSpellingDocument()));
a->setEnabled(textCursor().hasSelection());
a = menu->addAction(tr("Check Spelling Document"), this, SLOT(checkSpellingDocument()));
a->setEnabled(!document()->isEmpty() && !textCursor().hasSelection());
menu->exec(e->globalPos());
delete menu;
}

bool LatexEditor::search( const QString &expr, bool cs, bool wo, bool forward, bool startAtCursor )
{
QTextDocument::FindFlags flags = 0;
if (cs) flags |= QTextDocument::FindCaseSensitively;
if (wo) flags |= QTextDocument::FindWholeWords;
QTextCursor c = textCursor();
//if (!c.hasSelection())
// {
// if (forward) c.movePosition(QTextCursor::Start);
// else c.movePosition(QTextCursor::End);
// setTextCursor(c);
// }
QTextDocument::FindFlags options;
if (! startAtCursor)
{
c.movePosition(QTextCursor::Start);
setTextCursor(c);
}
if (forward == false) flags |= QTextDocument::FindBackward;
QTextCursor found = document()->find(expr, c, flags);

if (found.isNull()) return false;
else
{
setTextCursor(found);
return true;
}
}

void LatexEditor::replace( const QString &r)
{
int start;
QTextCursor c = textCursor();
if (c.hasSelection())
{
start=c.selectionStart();
c.removeSelectedText();
c.insertText(r);
c.setPosition(start,QTextCursor::MoveAnchor);
c.setPosition(start+r.length(),QTextCursor::KeepAn chor);
// c.movePosition(QTextCursor::NextWord,QTextCursor:: KeepAnchor);
setTextCursor(c);
}
}

void LatexEditor::gotoLine( int line )
{
if (line<=numoflines()) setCursorPosition( line, 0 );
}

void LatexEditor::commentSelection()
{
bool go=true;
QTextCursor cur=textCursor();
if (cur.hasSelection())
{
int start=cur.selectionStart();
int end=cur.selectionEnd();
cur.setPosition(start,QTextCursor::MoveAnchor);
cur.movePosition(QTextCursor::StartOfBlock,QTextCu rsor::MoveAnchor);
while ( cur.position() < end && go)
{
cur.insertText("%");
end++;
go=cur.movePosition(QTextCursor::NextBlock,QTextCu rsor::MoveAnchor);
}
}
}

void LatexEditor::indentSelection()
{
bool go=true;
QTextCursor cur=textCursor();
if (cur.hasSelection())
{
int start=cur.selectionStart();
int end=cur.selectionEnd();
cur.setPosition(start,QTextCursor::MoveAnchor);
cur.movePosition(QTextCursor::StartOfBlock,QTextCu rsor::MoveAnchor);
while ( cur.position() < end && go)
{
cur.insertText("\t");
end++;
go=cur.movePosition(QTextCursor::NextBlock,QTextCu rsor::MoveAnchor);
}
}
}

void LatexEditor::uncommentSelection()
{
bool go=true;
QTextCursor cur=textCursor();
if (cur.hasSelection())
{
int start=cur.selectionStart();
int end=cur.selectionEnd();
cur.setPosition(start,QTextCursor::MoveAnchor);
cur.movePosition(QTextCursor::StartOfBlock,QTextCu rsor::MoveAnchor);
while ( cur.position() < end && go)
{
cur.movePosition(QTextCursor::Right,QTextCursor::K eepAnchor);
if (cur.selectedText()=="%")
{
cur.removeSelectedText();
end--;
}
go=cur.movePosition(QTextCursor::NextBlock,QTextCu rsor::MoveAnchor);
}
}
}

void LatexEditor::unindentSelection()
{
bool go=true;
QTextCursor cur=textCursor();
if (cur.hasSelection())
{
int start=cur.selectionStart();
int end=cur.selectionEnd();
cur.setPosition(start,QTextCursor::MoveAnchor);
cur.movePosition(QTextCursor::StartOfBlock,QTextCu rsor::MoveAnchor);
while ( cur.position() < end && go)
{
cur.movePosition(QTextCursor::NextCharacter,QTextC ursor::KeepAnchor);
if (cur.selectedText()=="\t")
{
cur.removeSelectedText();
end--;
}
go=cur.movePosition(QTextCursor::NextBlock,QTextCu rsor::MoveAnchor);
}
}
}

void LatexEditor::changeFont(QFont & new_font)
{
setFont(new_font);
}

QString LatexEditor::getEncoding()
{
return encoding;
}

void LatexEditor::setEncoding(QString enc)
{
encoding=enc;
}

int LatexEditor::getCursorPosition(int para, int index)
{
int i = 0;
QTextBlock p = document()->begin();
while ( p.isValid() )
{
if (para==i) break;
i++;
p = p.next();
}
return p.position()+index;
}

void LatexEditor::setCursorPosition(int para, int index)
{
int pos=getCursorPosition(para,index);
QTextCursor cur=textCursor();
cur.setPosition(pos,QTextCursor::MoveAnchor);
setTextCursor(cur);
ensureCursorVisible();
setFocus();
}

void LatexEditor::removeOptAlt()
{
QTextCursor cur=textCursor();
QTextBlock p = document()->begin();
QString s;
while (p.isValid())
{
s = p.text();
s=s.left(3);
if (s=="OPT" || s=="ALT")
{
int pos=p.position();
p = p.next();
cur.setPosition(pos,QTextCursor::MoveAnchor);
cur.select(QTextCursor::BlockUnderCursor);
cur.removeSelectedText();
}
else
{
p = p.next();
}
}
setFocus();
}

int LatexEditor::numoflines()
{
int num=0;
QTextBlock p;
for (p = document()->begin(); p.isValid(); p = p.next()) ++num;
return num;
}

int LatexEditor::linefromblock(const QTextBlock& p)
{
if (!p.isValid()) return -1;
int num = 1;
QTextBlock block=document()->begin();
while (block.isValid())
{
if ( p == block ) return num;
num++;
block = block.next();
}
return -1;
}

void LatexEditor::selectword(int line, int col, QString word)
{
QTextCursor cur=textCursor();
int i = 0;
QTextBlock p = document()->begin();
while ( p.isValid() )
{
if (line==i) break;
i++;
p = p.next();
}
int pos=p.position();
int offset=word.length();
cur.setPosition(pos+col,QTextCursor::MoveAnchor);
cur.setPosition(pos+col+offset,QTextCursor::KeepAn chor);
setTextCursor(cur);
ensureCursorVisible();
}

void LatexEditor::checkSpellingWord()
{
QTextCursor cur=textCursor();
cur.select(QTextCursor::WordUnderCursor);
setTextCursor(cur);
if (cur.hasSelection()) emit spellme();
}

void LatexEditor::checkSpellingDocument()
{
emit spellme();
}

QString LatexEditor::textUnderCursor() const
{
QTextCursor tc = textCursor();
int oldpos=tc.position();
tc.select(QTextCursor::WordUnderCursor);
int newpos = tc.selectionStart();
tc.setPosition(newpos, QTextCursor::MoveAnchor);
tc.setPosition(oldpos, QTextCursor::KeepAnchor);
QString word=tc.selectedText();
QString sword=word.trimmed();
if (word.right(1)!=sword.right(1)) word="";
return word;
}

void LatexEditor::keyPressEvent ( QKeyEvent * e )
{
if (c && c->popup()->isVisible())
{
// The following keys are forwarded by the completer to the widget
switch (e->key())
{
case Qt::Key_Enter:
case Qt::Key_Return:
case Qt::Key_Escape:
case Qt::Key_Tab:
case Qt::Key_Backtab:
e->ignore();
return; // let the completer do default behavior
default:
break;
}
}

//bool isShortcut = ((e->modifiers() & Qt::ControlModifier) && e->key() == Qt::Key_E); // CTRL+E
//if (!c || !isShortcut)
// {
if ((e->key()==Qt::Key_Backtab))
{
QTextCursor cursor=textCursor();
cursor.movePosition(QTextCursor::PreviousCharacter ,QTextCursor::KeepAnchor);
if (cursor.selectedText()=="\t")
{
cursor.removeSelectedText();
}
}
else if ((e->key()==Qt::Key_Enter)||(e->key()==Qt::Key_Return))
{
QTextEdit::keyPressEvent(e);
QTextCursor cursor=textCursor();
cursor.joinPreviousEditBlock();
QTextBlock block=cursor.block();
QTextBlock blockprev=block.previous();
if (blockprev.isValid())
{
QString txt=blockprev.text();
int j=0;
while ( (j<txt.count()) && ((txt[j]==' ') || txt[j]=='\t') )
{
cursor.insertText(QString(txt[j]));
j++;
}
}
cursor.endEditBlock();
}
else QTextEdit::keyPressEvent(e);
// }// dont process the shortcut when we have a completer

const bool ctrlOrShift = e->modifiers() & (Qt::ControlModifier | Qt::ShiftModifier);
if (!c || (ctrlOrShift && e->text().isEmpty())) return;

//static QString eow("~!@#$%^&*()_+{}|:\"<>?,./;'[]\\-=");
static QString eow("~!@#$%^&*()_+{}|:\"<>?,./;'[]-= "); // end of word
bool hasModifier = (e->modifiers() & ( Qt::ControlModifier | Qt::AltModifier ));
//bool hasModifier = (e->modifiers() != Qt::NoModifier) && !ctrlOrShift;
QString completionPrefix = textUnderCursor();
if (hasModifier || e->text().isEmpty()|| completionPrefix.length() < 3 || eow.contains(e->text().right(1)))
//if (!isShortcut && (hasModifier || e->text().isEmpty()|| completionPrefix.length() < 3 || eow.contains(e->text().right(1))))
{
c->popup()->hide();
return;
}
if (completionPrefix != c->completionPrefix())
{
c->setCompletionPrefix(completionPrefix);
c->popup()->setCurrentIndex(c->completionModel()->index(0, 0));
}
QRect cr = cursorRect();
cr.setWidth(c->popup()->sizeHintForColumn(0)+ c->popup()->verticalScrollBar()->sizeHint().width());
c->complete(cr); // popup it up!
}

QCompleter *LatexEditor::completer() const
{
return c;
}

void LatexEditor::setCompleter(QCompleter *completer)
{
if (c) QObject::disconnect(c, 0, this, 0);
c = completer;
if (!c) return;
c->setWidget(this);
c->setCompletionMode(QCompleter::PopupCompletion);
c->setCaseSensitivity(Qt::CaseInsensitive);
QObject::connect(c, SIGNAL(activated(const QString&)),this, SLOT(insertCompletion(const QString&)));
}

void LatexEditor::insertCompletion(const QString& completion)
{
if (c->widget() != this) return;
QTextCursor tc = textCursor();
int extra = completion.length() - c->completionPrefix().length();
//tc.movePosition(QTextCursor::Left);
//tc.movePosition(QTextCursor::EndOfWord);
int pos=tc.position();
QString insert_word = "";
QString original_word=completion.right(extra);
bool skipfirst=(completion.startsWith("\\begin{") || completion.startsWith("\\end{") || completion.startsWith("\\ref{") || completion.startsWith("\\pageref{"));
QString character;
bool ignore = false;
int offset=0;
for ( int i = 0; i < original_word.length(); ++i )
{
character=original_word.mid(i,1);
if (character=="[" || character=="{" || character=="(" || character=="<")
{
insert_word += character;
if (!skipfirst)
{
ignore = true;
if (offset==0) offset=i;
}
else skipfirst=false;
}
else if (character=="]" || character=="}" || character==")" || character==">")
{
insert_word += character;
ignore = false;
}
else if (character==",")
{
insert_word += character;
}
else if ( ! ignore ) insert_word += character;
}
tc.insertText(insert_word);
if (offset!=0) tc.setPosition(pos+offset+1,QTextCursor::MoveAncho r);
setTextCursor(tc);
}

void LatexEditor::focusInEvent(QFocusEvent *e)
{
if (c) c->setWidget(this);
QTextEdit::focusInEvent(e);
}