| 1 |
/**************************************************************************************** |
| 2 |
* Copyright (c) 2009 Thomas Lübking <[email protected] * |
| 3 |
* * |
| 4 |
* This program is free software; you can redistribute it and/or modify it under * |
| 5 |
* the terms of the GNU General Public License as published by the Free Software * |
| 6 |
* Foundation; either version 2 of the License, or (at your option) any later * |
| 7 |
* version. * |
| 8 |
* * |
| 9 |
* This program is distributed in the hope that it will be useful, but WITHOUT ANY * |
| 10 |
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A * |
| 11 |
* PARTICULAR PURPOSE. See the GNU General Public License for more details. * |
| 12 |
* * |
| 13 |
* You should have received a copy of the GNU General Public License along with * |
| 14 |
* this program. If not, see <http://www.gnu.org/licenses/>. * |
| 15 |
****************************************************************************************/ |
| 16 |
|
| 17 |
#include "LayoutEditDialog.h" |
| 18 |
|
| 19 |
#include "widgets/TokenWithLayout.h" |
| 20 |
#include "widgets/TokenDropTarget.h" |
| 21 |
|
| 22 |
#include <KLocalizedString> |
| 23 |
|
| 24 |
#include <QButtonGroup> |
| 25 |
#include <QComboBox> |
| 26 |
#include <QDialogButtonBox> |
| 27 |
#include <QHBoxLayout> |
| 28 |
#include <QIcon> |
| 29 |
#include <QLabel> |
| 30 |
#include <QLineEdit> |
| 31 |
#include <QPainter> |
| 32 |
#include <QRadioButton> |
| 33 |
#include <QSlider> |
| 34 |
#include <QStyle> |
| 35 |
#include <QStyleOptionFrame> |
| 36 |
#include <QToolButton> |
| 37 |
|
| 38 |
|
| 39 |
class HintingLineEdit : public QLineEdit |
| 40 |
{ |
| 41 |
public: |
| 42 |
HintingLineEdit( const QString &hint = QString(), QWidget *parent = nullptr ) : QLineEdit( parent ), m_hint( hint ) |
| 43 |
{ } |
| 44 |
void setHint( const QString &hint ) |
| 45 |
{ |
| 46 |
m_hint = hint; |
| 47 |
} |
| 48 |
protected: |
| 49 |
void paintEvent ( QPaintEvent *pe ) override |
| 50 |
{ |
| 51 |
QLineEdit::paintEvent( pe ); |
| 52 |
if ( !hasFocus() && text().isEmpty() ) |
| 53 |
{ |
| 54 |
QStyleOptionFrame opt; |
| 55 |
initStyleOption( &opt ); |
| 56 |
|
| 57 |
QPainter p(this); |
| 58 |
QColor fg = palette().color( foregroundRole() ); |
| 59 |
fg.setAlpha( fg.alpha() / 2 ); |
| 60 |
p.setPen( fg ); |
| 61 |
|
| 62 |
p.drawText( style()->subElementRect( QStyle::SE_LineEditContents, &opt, this ), |
| 63 |
alignment() | Qt::TextSingleLine | Qt::TextIncludeTrailingSpaces, |
| 64 |
m_hint ); |
| 65 |
p.end(); |
| 66 |
} |
| 67 |
} |
| 68 |
private: |
| 69 |
QString m_hint; |
| 70 |
}; |
| 71 |
|
| 72 |
LayoutEditDialog::LayoutEditDialog( QWidget *parent ) : QDialog( parent ) |
| 73 |
{ |
| 74 |
setWindowTitle( i18n( "Configuration for" ) ); |
| 75 |
|
| 76 |
QFont boldFont = font(); |
| 77 |
boldFont.setBold( true ); |
| 78 |
QVBoxLayout *l1 = new QVBoxLayout( this ); |
| 79 |
QHBoxLayout *l2 = new QHBoxLayout; |
| 80 |
l2->addWidget( m_prefix = new HintingLineEdit( i18nc( "placeholder for a prefix", "[prefix]" ), this ) ); |
| 81 |
m_prefix->setAlignment( Qt::AlignRight | Qt::AlignVCenter ); |
| 82 |
l2->addWidget( m_element = new QLabel( this ) ); |
| 83 |
m_element->setFont( boldFont ); |
| 84 |
l2->addWidget( m_suffix = new HintingLineEdit( i18nc( "placeholder for a suffix", "[suffix]" ), this ) ); |
| 85 |
l1->addLayout( l2 ); |
| 86 |
|
| 87 |
QFrame *line = new QFrame( this ); |
| 88 |
line->setFrameStyle( QFrame::Sunken | QFrame::HLine ); |
| 89 |
l1->addWidget( line ); |
| 90 |
|
| 91 |
QWidget *boxWidget = new QWidget( this ); |
| 92 |
QLabel *l; |
| 93 |
|
| 94 |
#define HAVE_WIDTH_MODES 0 |
| 95 |
|
| 96 |
QHBoxLayout *l4 = new QHBoxLayout; |
| 97 |
l = new QLabel( i18n( "Width: " ), this ); |
| 98 |
l->setFont( boldFont ); |
| 99 |
l4->addWidget( l ); |
| 100 |
l4->addWidget( m_fixedWidth = new QRadioButton( i18n( "Custom" ), this ) ); |
| 101 |
m_fixedWidth->setToolTip( i18n( "Either a fixed (absolute) value, or a relative value (e.g. 128px or 12%)." ) ); |
| 102 |
m_fixedWidth->setChecked( true ); |
| 103 |
#if HAVE_WIDTH_MODES |
| 104 |
l4->addWidget( m_fitContent = new QRadioButton( i18n( "Fit content" ), this ) ); |
| 105 |
m_fitContent->setToolTip( i18n( "Fit the element text" ) ); |
| 106 |
#endif |
| 107 |
l4->addWidget( m_automaticWidth = new QRadioButton( i18nc( "automatic width", "Automatic" ), this ) ); |
| 108 |
m_automaticWidth->setToolTip( i18n( "Take homogeneous part of the space available to all elements with automatic width" ) ); |
| 109 |
l4->addStretch(); |
| 110 |
connect( m_fixedWidth, &QRadioButton::toggled, boxWidget, &QWidget::setEnabled ); |
| 111 |
connect( m_automaticWidth, &QRadioButton::toggled, this, &LayoutEditDialog::setAutomaticWidth ); |
| 112 |
l1->addLayout( l4 ); |
| 113 |
|
| 114 |
QHBoxLayout *l5 = new QHBoxLayout( boxWidget ); |
| 115 |
l5->addWidget( m_width = new QSlider( Qt::Horizontal, boxWidget ) ); |
| 116 |
m_width->setRange( 0, 100 ); |
| 117 |
l = new QLabel( boxWidget ); |
| 118 |
l5->addWidget( l ); |
| 119 |
// width->connect( sizeMode, SIGNAL(currentIndexChanged(int)), SLOT(setDisabled()) ) |
| 120 |
l->setNum( 0 ); |
| 121 |
connect( m_width, &QSlider::valueChanged, l, QOverload<int>::of(&QLabel::setNum) ); |
| 122 |
|
| 123 |
#define HAVE_METRICS 0 |
| 124 |
#if HAVE_METRICS |
| 125 |
QComboBox *metrics = new QComboBox( this ); |
| 126 |
metrics->setFrame( false ); |
| 127 |
metrics->addItem( "%" ); |
| 128 |
metrics->addItem( "px" ); |
| 129 |
metrics->addItem( "chars" ); |
| 130 |
l5->addWidget( metrics ); |
| 131 |
#else |
| 132 |
QLabel *metrics = new QLabel( QStringLiteral("%"), this ); |
| 133 |
l5->addWidget( metrics ); |
| 134 |
#endif |
| 135 |
|
| 136 |
l1->addWidget( boxWidget ); |
| 137 |
|
| 138 |
line = new QFrame( this ); |
| 139 |
line->setFrameStyle( QFrame::Sunken | QFrame::HLine ); |
| 140 |
l1->addWidget( line ); |
| 141 |
|
| 142 |
QHBoxLayout *l3 = new QHBoxLayout; |
| 143 |
l = new QLabel( i18n( "Alignment: " ), this ); |
| 144 |
l->setFont( boldFont ); |
| 145 |
l3->addWidget( l ); |
| 146 |
l3->addWidget( m_alignLeft = new QToolButton( this ) ); |
| 147 |
l3->addWidget( m_alignCenter = new QToolButton( this ) ); |
| 148 |
l3->addWidget( m_alignRight = new QToolButton( this ) ); |
| 149 |
|
| 150 |
l3->addSpacing( 12 ); |
| 151 |
|
| 152 |
l = new QLabel( i18n( "Font: " ), this ); |
| 153 |
l->setFont( boldFont ); |
| 154 |
l3->addWidget( l ); |
| 155 |
l3->addWidget( m_bold = new QToolButton( this ) ); |
| 156 |
l3->addWidget( m_italic = new QToolButton( this ) ); |
| 157 |
l3->addWidget( m_underline = new QToolButton( this ) ); |
| 158 |
l3->addStretch(); |
| 159 |
l1->addLayout( l3 ); |
| 160 |
|
| 161 |
QDialogButtonBox *box = new QDialogButtonBox(this); |
| 162 |
box->addButton( QDialogButtonBox::Cancel ); |
| 163 |
box->addButton( QDialogButtonBox::Ok ); |
| 164 |
connect( box, &QDialogButtonBox::rejected, this, &LayoutEditDialog::close ); |
| 165 |
connect( box, &QDialogButtonBox::accepted, this, &LayoutEditDialog::apply ); |
| 166 |
l1->addWidget( box ); |
| 167 |
|
| 168 |
l1->addStretch(); |
| 169 |
|
| 170 |
m_alignLeft->setIcon( QIcon::fromTheme( QStringLiteral("format-justify-left") ) ); |
| 171 |
m_alignLeft->setCheckable( true ); |
| 172 |
m_alignCenter->setIcon( QIcon::fromTheme( QStringLiteral("format-justify-center") ) ); |
| 173 |
m_alignCenter->setCheckable( true ); |
| 174 |
m_alignRight->setIcon( QIcon::fromTheme( QStringLiteral("format-justify-right") ) ); |
| 175 |
m_alignRight->setCheckable( true ); |
| 176 |
QButtonGroup *align = new QButtonGroup( this ); |
| 177 |
align->setExclusive( true ); |
| 178 |
align->addButton( m_alignLeft ); |
| 179 |
align->addButton( m_alignCenter ); |
| 180 |
align->addButton( m_alignRight ); |
| 181 |
|
| 182 |
m_bold->setIcon( QIcon::fromTheme( QStringLiteral("format-text-bold") ) ); |
| 183 |
m_bold->setCheckable( true ); |
| 184 |
m_italic->setIcon( QIcon::fromTheme( QStringLiteral("format-text-italic") ) ); |
| 185 |
m_italic->setCheckable( true ); |
| 186 |
m_underline->setIcon( QIcon::fromTheme( QStringLiteral("format-text-underline") ) ); |
| 187 |
m_underline->setCheckable( true ); |
| 188 |
|
| 189 |
} |
| 190 |
|
| 191 |
void LayoutEditDialog::apply() |
| 192 |
{ |
| 193 |
if( !m_token ) |
| 194 |
return; |
| 195 |
|
| 196 |
m_token->setPrefix( m_prefix->text() ); |
| 197 |
m_token->setSuffix( m_suffix->text() ); |
| 198 |
m_token->setWidth( m_width->value() ); |
| 199 |
if ( m_alignLeft->isChecked() ) |
| 200 |
m_token->setAlignment( Qt::AlignLeft ); |
| 201 |
else if ( m_alignCenter->isChecked() ) |
| 202 |
m_token->setAlignment( Qt::AlignHCenter ); |
| 203 |
else if ( m_alignRight->isChecked() ) |
| 204 |
m_token->setAlignment( Qt::AlignRight ); |
| 205 |
m_token->setBold( m_bold->isChecked() ); |
| 206 |
m_token->setItalic( m_italic->isChecked() ); |
| 207 |
m_token->setUnderline( m_underline->isChecked() ); |
| 208 |
|
| 209 |
// we do this here to avoid reliance on the connection order (i.e. prevent close before apply) |
| 210 |
if( sender() ) |
| 211 |
close(); |
| 212 |
} |
| 213 |
|
| 214 |
void LayoutEditDialog::close() |
| 215 |
{ |
| 216 |
m_token.clear(); |
| 217 |
QDialog::close(); |
| 218 |
} |
| 219 |
|
| 220 |
void LayoutEditDialog::setAutomaticWidth( bool automatic ) |
| 221 |
{ |
| 222 |
if( automatic ) |
| 223 |
{ |
| 224 |
m_previousWidth = m_width->value(); |
| 225 |
m_width->setMinimum( 0 ); // without setting the minimum we can't set the value.. |
| 226 |
m_width->setValue( 0 ); // automatic width is represented by width == 0 |
| 227 |
} |
| 228 |
else |
| 229 |
{ |
| 230 |
m_width->setValue( m_previousWidth ); |
| 231 |
m_width->setMinimum( 1 ); // set minimum back to 1 since "0" means automatic |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
void LayoutEditDialog::setToken( TokenWithLayout *t ) |
| 237 |
{ |
| 238 |
m_token = t; |
| 239 |
if ( !m_token ) |
| 240 |
return; |
| 241 |
setWindowTitle( i18n( "Configuration for '%1'", t->name() ) ); |
| 242 |
|
| 243 |
m_element->setText( m_token->name() ); |
| 244 |
m_prefix->setText( m_token->prefix() ); |
| 245 |
m_suffix->setText( m_token->suffix() ); |
| 246 |
|
| 247 |
|
| 248 |
// Compute the remaining space from the tokens on the same line. |
| 249 |
// this should still not be done here as it makes upward assumptions |
| 250 |
// solution(?) token->element->row->elements |
| 251 |
TokenDropTarget *editWidget = qobject_cast<TokenDropTarget*>( m_token->parentWidget() ); |
| 252 |
if( editWidget ) |
| 253 |
{ |
| 254 |
qreal spareWidth = 100.0; |
| 255 |
int row = editWidget->row( m_token.data() ); |
| 256 |
if( row > -1 ) |
| 257 |
{ |
| 258 |
QList<Token*> tokens = editWidget->tokensAtRow( row ); |
| 259 |
for ( Token *token : tokens ) |
| 260 |
{ |
| 261 |
if ( token == m_token.data() ) |
| 262 |
continue; |
| 263 |
|
| 264 |
if ( TokenWithLayout *twl = qobject_cast<TokenWithLayout*>( token ) ) |
| 265 |
spareWidth -= twl->width() * 100.0; |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
int max = qMax( spareWidth, qreal( 0.0 ) ); |
| 270 |
|
| 271 |
if( max >= m_token->width() * 100.0 ) |
| 272 |
m_width->setMaximum( qMax( spareWidth, qreal( 0.0 ) ) ); |
| 273 |
else |
| 274 |
m_width->setMaximum( m_token->width() * 100.0 ); |
| 275 |
} |
| 276 |
m_width->setValue( m_token->width() * 100.0 ); |
| 277 |
m_previousWidth = m_width->value(); |
| 278 |
|
| 279 |
if ( m_token->width() > 0.0 ) |
| 280 |
m_fixedWidth->setChecked( true ); |
| 281 |
else |
| 282 |
m_automaticWidth->setChecked( true ); |
| 283 |
|
| 284 |
if ( m_token->alignment() & Qt::AlignLeft ) |
| 285 |
m_alignLeft->setChecked(true); |
| 286 |
else if ( m_token->alignment() & Qt::AlignHCenter ) |
| 287 |
m_alignCenter->setChecked(true); |
| 288 |
else if ( m_token->alignment() & Qt::AlignRight ) |
| 289 |
m_alignRight->setChecked(true); |
| 290 |
|
| 291 |
m_bold->setChecked( m_token->bold() ); |
| 292 |
m_italic->setChecked( m_token->italic() ); |
| 293 |
m_underline->setChecked( m_token->underline() ); |
| 294 |
} |
| 295 |
|