void TextEdit::IndentText( bool make ) /* make == keyPressEvent tab + false - */
{
/* baseram = QApplication::clipboard(); */
/* no text selection move tabulator */
if ( make && c.selectedText().isEmpty() )
{
if ( m_tabSpaces )
{
int nbSpaces = tabStopWidth() / fontMetrics().width( " " );
for (int i = 0; i<nbSpaces; i++) {
c.insertText( " " );
}
}
else {
if ( m_tabSpaces )
{
int nbSpaces = tabStopWidth() / fontMetrics().width( " " );
for (int i = 0; i<nbSpaces; i++) {
c.insertText( " " );
}
}
else {
c.insertText( "\t" );
}
}
return;
} else
/* having text to tabulator */
if ( make && !c.selectedText().isEmpty() ) {
int startb = c.selectionStart();
int endb = c.selectionEnd();
int position = 0;
int startline;
int stopline;
int xli = 1;
for ( QTextBlock block
= document
()->begin
(); block.
isValid(); block
= block.
next(), xli
++) { if (blocStart.text() == block.text()) {
startline = xli;
}
if (blocEnd.text() == block.text()) {
stopline = xli;
}
}
xli = 1;
if (stopline > startline) {
//////qDebug() << "### ok line greats ";
QString greppitxt
= GrepTextLines
(startline,stopline
);
//////qDebug() << "### txt indent " << greppitxt;
if (greppitxt.size() > 0) {
cut();
baseram->clear();
baseram
->setText
(greppitxt,
QClipboard::Clipboard);
paste();
selectLines(startline,stopline);
c1.
setPosition(c1.
position() - 1 ,
QTextCursor::KeepAnchor);
setTextCursor( c1 );
ensureCursorVisible();
}
return;
} else if ( stopline == startline ) {
//////qDebug() << "### ok line greats ";
QString greppitxto
= GrepTextLines
(startline,startline
);
//////qDebug() << "### txt indent " << greppitxt;
if (greppitxto.size() > 0) {
cut();
baseram->clear();
baseram
->setText
(greppitxto,
QClipboard::Clipboard);
paste();
selectLines(startline,startline);
c2.
setPosition(c2.
position() - 1 ,
QTextCursor::KeepAnchor);
setTextCursor( c2 );
ensureCursorVisible();
return;
}
}
}
}
QString TextEdit
::GrepTextLines(int startline,
int stopline
) {
cuttextlist.clear();
int xli = 1;
for ( QTextBlock block
= document
()->begin
(); block.
isValid(); block
= block.
next(), xli
++) { if (xli == startline || xli > startline) {
if (xli == stopline || xli < stopline) {
cuttextlist.append(GrepLineFromTxBloc(block));
////////qDebug() << "### cat line GrepTextLines " << xli;
}
}
}
return cuttextlist.join("\n");
}
{
if ( m_tabSpaces ) /* if tabulator key on? */
{
int nbSpaces = tabStopWidth() / fontMetrics().width( " " );
for (int i = 0; i<nbSpaces; i++) {
linetext.append( " " );
}
linetext.append(block.text());
} else {
linetext.append(block.text());
}
return linetext;
}
void TextEdit::selectLines(int debut, int fin) /* start end */
{
if ( debut > fin )
qSwap( debut, fin);
setTextCursor( c );
ensureCursorVisible();
}
void TextEdit::IndentText( bool make ) /* make == keyPressEvent tab + false - */
{
/* baseram = QApplication::clipboard(); */
QTextCursor curseurActuel = textCursor();
QTextCursor c = textCursor();
/* no text selection move tabulator */
if ( make && c.selectedText().isEmpty() )
{
if ( m_tabSpaces )
{
int nbSpaces = tabStopWidth() / fontMetrics().width( " " );
for (int i = 0; i<nbSpaces; i++) {
c.insertText( " " );
}
}
else {
if ( m_tabSpaces )
{
int nbSpaces = tabStopWidth() / fontMetrics().width( " " );
for (int i = 0; i<nbSpaces; i++) {
c.insertText( " " );
}
}
else {
c.insertText( "\t" );
}
}
return;
} else
/* having text to tabulator */
if ( make && !c.selectedText().isEmpty() ) {
int startb = c.selectionStart();
int endb = c.selectionEnd();
c.setPosition(startb, QTextCursor::MoveAnchor);
c.movePosition(QTextCursor::StartOfLine, QTextCursor::MoveAnchor);
QTextBlock blocStart = c.block();
c.setPosition(endb, QTextCursor::MoveAnchor);
c.movePosition(QTextCursor::StartOfLine, QTextCursor::MoveAnchor);
QTextBlock blocEnd = c.block();
int position = 0;
int startline;
int stopline;
int xli = 1;
for ( QTextBlock block = document()->begin(); block.isValid(); block = block.next(), xli++) {
if (blocStart.text() == block.text()) {
startline = xli;
}
if (blocEnd.text() == block.text()) {
stopline = xli;
}
}
xli = 1;
if (stopline > startline) {
//////qDebug() << "### ok line greats ";
QString greppitxt = GrepTextLines(startline,stopline);
//////qDebug() << "### txt indent " << greppitxt;
if (greppitxt.size() > 0) {
cut();
baseram->clear();
baseram->setText(greppitxt,QClipboard::Clipboard);
paste();
selectLines(startline,stopline);
QTextCursor c1 = textCursor();
c1.setPosition(c1.position() - 1 ,QTextCursor::KeepAnchor);
setTextCursor( c1 );
ensureCursorVisible();
}
return;
} else if ( stopline == startline ) {
//////qDebug() << "### ok line greats ";
QString greppitxto = GrepTextLines(startline,startline);
//////qDebug() << "### txt indent " << greppitxt;
if (greppitxto.size() > 0) {
cut();
baseram->clear();
baseram->setText(greppitxto,QClipboard::Clipboard);
paste();
selectLines(startline,startline);
QTextCursor c2 = textCursor();
c2.setPosition(c2.position() - 1 ,QTextCursor::KeepAnchor);
setTextCursor( c2 );
ensureCursorVisible();
return;
}
}
}
}
QString TextEdit::GrepTextLines(int startline, int stopline )
{
QStringList cuttextlist;
cuttextlist.clear();
int xli = 1;
for ( QTextBlock block = document()->begin(); block.isValid(); block = block.next(), xli++) {
if (xli == startline || xli > startline) {
if (xli == stopline || xli < stopline) {
cuttextlist.append(GrepLineFromTxBloc(block));
////////qDebug() << "### cat line GrepTextLines " << xli;
}
}
}
return cuttextlist.join("\n");
}
QString TextEdit::GrepLineFromTxBloc( QTextBlock block )
{
QString linetext = "";
if ( m_tabSpaces ) /* if tabulator key on? */
{
int nbSpaces = tabStopWidth() / fontMetrics().width( " " );
for (int i = 0; i<nbSpaces; i++) {
linetext.append( " " );
}
linetext.append(block.text());
} else {
linetext.append(block.text());
}
return linetext;
}
void TextEdit::selectLines(int debut, int fin) /* start end */
{
if ( debut > fin )
qSwap( debut, fin);
QTextCursor c = textCursor();
c.movePosition(QTextCursor::Start );
c.movePosition(QTextCursor::Down, QTextCursor::MoveAnchor, debut - 1 );
c.movePosition(QTextCursor::Down, QTextCursor::KeepAnchor, fin-debut + 1);
setTextCursor( c );
ensureCursorVisible();
}
To copy to clipboard, switch view to plain text mode
Bookmarks