|
// header |
|
|
|
#include <qobject.h> |
|
#include <qwidget.h> |
|
#include <qpushbutton.h> |
|
|
|
class LineBreakButton : public QPushButton { |
|
Q_OBJECT |
|
|
|
public: |
|
LineBreakButton( QWidget *parent, const char *name ); |
|
~LineBreakButton(); |
|
|
|
virtual void setText( const QString &text ); |
|
}; |
|
|
|
|
|
// part of override implementation |
|
|
|
#include <qwidget.h> |
|
#include <qpushbutton.h> |
|
#include <qstring.h> |
|
#include <qstringlist.h> |
|
#include <qfontmetrics.h> |
|
|
|
#include "LineBreakButton.h" |
|
|
|
// |
|
// more trivial stuff here |
|
// |
|
|
|
void |
|
LineBreakButton::setText( const QString &text ) { |
|
QString ftext = text.copy(); |
|
QFontMetrics fm = QWidget::fontMetrics(); |
|
|
|
// split the desired text using any existing newline breaks |
|
QStringList lines = QStringList::split( "\n", ftext ); |
|
|
|
QStringList::iterator line = lines.begin(); |
|
|
|
while ( line != lines.end() ) { |
|
// use any spaces to further split into words each line that we currently hold |
|
if ( ( *line ).find( ' ' ) != -1 && fm.width( *line ) >= this->width() ) { |
|
QStringList words = QStringList::split( " ", *line ); |
|
QStringList::iterator word; |
|
QString phrase = ""; |
|
|
|
// try to fit as many words as possible without drawing text out of widget area |
|
for ( word = words.begin(); word != words.end(); word++ ) { |
|
if ( fm.width( phrase + *word + " " ) >= this->width() ) |
|
break; |
|
|
|
phrase += *word + " "; |
|
} |
|
|
|
// check if there is a single word by its own that exceeds widget width |
|
// if so, simply draw it and advance the word iterator |
|
if ( "" == phrase ) { |
|
phrase += *word; |
|
word++; |
|
} |
|
|
|
// insert the new phrase and advance to the next line |
|
line = lines.remove( line ); |
|
|
|
line = lines.insert( line, phrase ); |
|
|
|
// find the next line after the current one that we have processed |
|
QStringList::iterator nextline = lines.begin(); |
|
|
|
for ( ; nextline != lines.end() && nextline != line ; nextline++ ) |
|
; |
|
|
|
nextline++; |
|
|
|
// prepend the rest of the words (if any) to the next line item |
|
while ( word != words.end() ) { |
|
if ( nextline != lines.end() ) |
|
*nextline += " " + *word; |
|
else { |
|
lines.append( *word ); |
|
nextline--; |
|
} |
|
|
|
word++; |
|
} |
|
} // end of line processing |
|
|
|
line++; |
|
} |
|
|
|
QButton::setText( lines.join( "\n" ) ); |
|
|
|
return; |
|
} |