1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
|
/* This file is part of the KDE project
Copyright (C) 2005 Christian Nitschkowski <segfault_ii@web.de>
Copyright (C) 2005 Jaroslaw Staniek <js@iidea.pl>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; see the file COPYING. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "kexidblabel.h"
#include <qbitmap.h>
#include <qpainter.h>
#include <qdrawutil.h>
#include <qapplication.h>
#include <qtimer.h>
#include <kdebug.h>
#include <kimageeffect.h>
#include <kexidb/field.h>
#include <kexiutils/utils.h>
#define SHADOW_OFFSET_X 3
#define SHADOW_OFFSET_Y 3
#define SHADOW_FACTOR 16.0
#define SHADOW_OPACITY 50.0
#define SHADOW_AXIS_FACTOR 2.0
#define SHADOW_DIAGONAL_FACTOR 1.0
#define SHADOW_THICKNESS 1
//! @internal
class KexiDBInternalLabel : public QLabel {
friend class KexiDBLabel;
public:
KexiDBInternalLabel( KexiDBLabel* );
virtual ~KexiDBInternalLabel();
protected:
void updateFrame();
QImage makeShadow( const QImage& textImage, const QColor &bgColor, const QRect& boundingRect );
QRect getBounding( const QImage &image, const QRect& startRect );
// double defaultDecay( QImage& source, int i, int j );
KPixmap getShadowPixmap();
QRect m_shadowRect;
KexiDBLabel *m_parentLabel;
};
KexiDBInternalLabel::KexiDBInternalLabel( KexiDBLabel* parent )
: QLabel( parent )
, m_parentLabel(parent)
{
int a = alignment() | Qt::WordBreak;
a &= (0xffffff ^ Qt::AlignVertical_Mask);
a |= Qt::AlignTop;
setAlignment( a );
updateFrame();
}
void KexiDBInternalLabel::updateFrame()
{
setIndent(m_parentLabel->indent());
setMargin(m_parentLabel->margin());
setFont(m_parentLabel->font());
setFrameShadow(m_parentLabel->frameShadow());
setFrameShape(m_parentLabel->frameShape());
setFrameStyle(m_parentLabel->frameStyle());
setMidLineWidth(m_parentLabel->midLineWidth());
setLineWidth(m_parentLabel->lineWidth());
}
KexiDBInternalLabel::~KexiDBInternalLabel()
{
}
/*!
* This method is copied from kdebase/kdesktop/kshadowengine.cpp
* Some modifactions were made.
* --
* Christian Nitschkowski
*/
QImage KexiDBInternalLabel::makeShadow( const QImage& textImage,
const QColor &bgColor, const QRect& boundingRect )
{
QImage result;
QString origText( text() );
// create a new image for for the shaddow
const int w = textImage.width();
const int h = textImage.height();
// avoid calling these methods for every pixel
const int bgRed = bgColor.red();
const int bgGreen = bgColor.green();
const int bgBlue = bgColor.blue();
const int startX = boundingRect.x() + SHADOW_THICKNESS;
const int startY = boundingRect.y() + SHADOW_THICKNESS;
const int effectWidth = boundingRect.bottomRight().x() - SHADOW_THICKNESS;
const int effectHeight = boundingRect.bottomRight().y() - SHADOW_THICKNESS;
// const int period = (effectWidth - startX) / 10;
double alphaShadow;
/*
* This is the source pixmap
*/
QImage img = textImage.convertDepth( 32 );
/*
* Resize the image if necessary
*/
if ( ( result.width() != w ) || ( result.height() != h ) ) {
result.create( w, h, 32 );
}
// result.fill( 0 ); // all black
double realOpacity = SHADOW_OPACITY + QMIN(50.0/double(256.0-qGray(bgColor.rgb())), 50.0);
//int _h, _s, _v;
//.getHsv( &_h, &_s, &_v );
if (colorGroup().background()==Qt::red)//_s>=250 && _v>=250) //for colors like cyan or red, make the result more white
realOpacity += 50.0;
result.fill( (int)realOpacity );
result.setAlphaBuffer( true );
for ( int i = startX; i < effectWidth; i++ ) {
for ( int j = startY; j < effectHeight; j++ ) {
/*!
* This method is copied from kdebase/kdesktop/kshadowengine.cpp
* Some modifactions were made.
* --
* Christian Nitschkowski
*/
if ( ( i < 1 ) || ( j < 1 ) || ( i > img.width() - 2 ) || ( j > img.height() - 2 ) )
continue;
else
alphaShadow = ( qGray( img.pixel( i - 1, j - 1 ) ) * SHADOW_DIAGONAL_FACTOR +
qGray( img.pixel( i - 1, j ) ) * SHADOW_AXIS_FACTOR +
qGray( img.pixel( i - 1, j + 1 ) ) * SHADOW_DIAGONAL_FACTOR +
qGray( img.pixel( i , j - 1 ) ) * SHADOW_AXIS_FACTOR +
0 +
qGray( img.pixel( i , j + 1 ) ) * SHADOW_AXIS_FACTOR +
qGray( img.pixel( i + 1, j - 1 ) ) * SHADOW_DIAGONAL_FACTOR +
qGray( img.pixel( i + 1, j ) ) * SHADOW_AXIS_FACTOR +
qGray( img.pixel( i + 1, j + 1 ) ) * SHADOW_DIAGONAL_FACTOR ) / SHADOW_FACTOR;
// update the shadow's i,j pixel.
if (alphaShadow > 0)
result.setPixel( i, j, qRgba( bgRed, bgGreen , bgBlue,
( int ) (( alphaShadow > realOpacity ) ? realOpacity : alphaShadow)
) );
}
/*caused too much redraw problems if (period && i % period) {
qApp->processEvents();
if (text() != origText) //text has been changed in the meantime: abort
return QImage();
}*/
}
return result;
}
KPixmap KexiDBInternalLabel::getShadowPixmap() {
/*!
* Backup the default color used to draw text.
*/
const QColor textColor = colorGroup().foreground();
/*!
* Temporary storage for the generated shadow
*/
KPixmap finalPixmap, tempPixmap;
QImage shadowImage, tempImage;
QPainter painter;
m_shadowRect = QRect();
tempPixmap.resize( size() );
tempPixmap.fill( Qt::black );
tempPixmap.setMask( tempPixmap.createHeuristicMask( true ) );
/*!
* The textcolor has to be white for creating shadows!
*/
setPaletteForegroundColor( Qt::white );
/*!
Draw the label "as usual" in a pixmap
*/
painter.begin( &tempPixmap );
painter.setFont( font() );
drawContents( &painter );
painter.end();
setPaletteForegroundColor( textColor );
/*!
* Calculate the first bounding rect.
* This will fit around the unmodified text.
*/
shadowImage = tempPixmap;
tempPixmap.setMask( QBitmap() );
/*!
Get the first bounding rect.
This may speed up makeShadow later.
*/
m_shadowRect = getBounding( shadowImage, m_shadowRect );
/*!
* Enlarge the bounding rect to make sure the shadow
* will fit in.
* The new rect has to fit in the pixmap.
* I have to admit this isn't really nice code...
*/
m_shadowRect.setX( QMAX( m_shadowRect.x() - ( m_shadowRect.width() / 4 ), 0 ) );
m_shadowRect.setY( QMAX( m_shadowRect.y() - ( m_shadowRect.height() / 4 ), 0 ) );
m_shadowRect.setBottomRight( QPoint(
QMIN( m_shadowRect.x() + ( m_shadowRect.width() * 3 / 2 ), shadowImage.width() ),
QMIN( m_shadowRect.y() + ( m_shadowRect.height() * 3 / 2 ), shadowImage.height() ) ) );
shadowImage = makeShadow( shadowImage,
qGray( colorGroup().background().rgb() ) < 127 ? Qt::white : Qt::black,
m_shadowRect );
if (shadowImage.isNull())
return KPixmap();
/*!
Now get the final bounding rect.
*/
m_shadowRect = getBounding( shadowImage, m_shadowRect );
/*!
Paint the labels background in a new pixmap.
*/
finalPixmap.resize( size() );
painter.begin( &finalPixmap );
painter.fillRect( 0, 0, finalPixmap.width(), finalPixmap.height(),
palette().brush(
isEnabled() ? QPalette::Active : QPalette::Disabled,
QColorGroup::Background ) );
painter.end();
/*!
Copy the part of the background the shadow will be on
to another pixmap.
*/
tempPixmap.resize( m_shadowRect.size() );
if (!finalPixmap.isNull()) {
bitBlt( &tempPixmap, 0, 0, &finalPixmap,
m_shadowRect.x() + SHADOW_OFFSET_X,
m_shadowRect.y() + SHADOW_OFFSET_Y,
m_shadowRect.width(),
m_shadowRect.height() );
}
/*!
Replace the big background pixmap with the
part we could out just before.
*/
finalPixmap = tempPixmap;
/*!
Copy the "interesting" part of the shadow image
to a new image.
I tried to copy this to a pixmap directly,
but it didn't work correctly.
Maybe a Qt bug?
*/
tempImage = shadowImage.copy( m_shadowRect );
tempPixmap.convertFromImage( tempImage );
/*!
Anyways, merge the shadow with the background.
*/
if (!tempPixmap.isNull()) {
bitBlt( &finalPixmap, 0, 0, &tempPixmap );
}
/**
Now move the rect.
Don't do this before the shadow is copied from shadowImage!
*/
m_shadowRect.moveBy( SHADOW_OFFSET_X, SHADOW_OFFSET_Y );
return finalPixmap;
}
QRect KexiDBInternalLabel::getBounding( const QImage &image, const QRect& startRect ) {
QPoint topLeft;
QPoint bottomRight;
const int startX = startRect.x();
const int startY = startRect.y();
/*!
* Ugly beast to get the correct width and height
*/
const int width = QMIN( ( startRect.bottomRight().x() > 0
? startRect.bottomRight().x() : QCOORD_MAX ),
image.width() );
const int height = QMIN( ( startRect.bottomRight().y() > 0
? startRect.bottomRight().y() : QCOORD_MAX ),
image.height() );
/*!
Assume the first pixel has the color of the
background that has to be cut away.
Qt uses the four corner pixels to guess the
correct color, but in this case the topleft
pixel should be enough.
*/
QRgb trans = image.pixel( 0, 0 );
for ( int y = startY; y < height; y++ ) {
for ( int x = startX; x < width; x++ ) {
if ( image.pixel( x, y ) != trans ) {
topLeft.setY( y );
y = height;
break;
}
}
}
for ( int x = startX; x < width; x++ ) {
for ( int y = startY; y < height; y++ ) {
if ( image.pixel( x, y ) != trans ) {
topLeft.setX( x );
x = width;
break;
}
}
}
for ( int y = height - 1; y > topLeft.y(); y-- ) {
for ( int x = width - 1; x > topLeft.x(); x-- ) {
if ( image.pixel( x, y ) != trans ) {
bottomRight.setY( y + 1 );
y = 0;
break;
}
}
}
for ( int x = width - 1; x > topLeft.x(); x-- ) {
for ( int y = height - 1; y > topLeft.y(); y-- ) {
if ( image.pixel( x, y ) != trans ) {
bottomRight.setX( x + 1 );
x = 0;
break;
}
}
}
return QRect(
topLeft.x(),
topLeft.y(),
bottomRight.x() - topLeft.x(),
bottomRight.y() - topLeft.y() );
}
//=========================================================
//! @internal
class KexiDBLabel::Private
{
public:
Private()
: timer(0)
// , autonumberDisplayParameters(0)
, pixmapDirty( true )
, shadowEnabled( false )
, resizeEvent( false )
{
}
~Private() {}
KPixmap shadowPixmap;
QPoint shadowPosition;
KexiDBInternalLabel* internalLabel;
QTimer* timer;
QColor frameColor;
bool pixmapDirty : 1;
bool shadowEnabled : 1;
bool resizeEvent : 1;
};
//=========================================================
KexiDBLabel::KexiDBLabel( QWidget *parent, const char *name, WFlags f )
: QLabel( parent, name, f )
, KexiDBTextWidgetInterface()
, KexiFormDataItemInterface()
, d( new Private() )
{
init();
}
KexiDBLabel::KexiDBLabel( const QString& text, QWidget *parent, const char *name, WFlags f )
: QLabel( parent, name, f )
, KexiDBTextWidgetInterface()
, KexiFormDataItemInterface()
, d( new Private() )
{
init();
setText( text );
}
KexiDBLabel::~KexiDBLabel()
{
delete d;
}
void KexiDBLabel::init()
{
m_hasFocusableWidget = false;
d->internalLabel = new KexiDBInternalLabel( this );
d->internalLabel->hide();
d->frameColor = palette().active().foreground();
setAlignment( d->internalLabel->alignment() );
}
void KexiDBLabel::updatePixmapLater() {
if (d->resizeEvent) {
if (!d->timer) {
d->timer = new QTimer(this, "KexiDBLabelTimer");
connect(d->timer, SIGNAL(timeout()), this, SLOT(updatePixmap()));
}
d->timer->start(100, true);
d->resizeEvent = false;
return;
}
if (d->timer && d->timer->isActive())
return;
updatePixmap();
}
void KexiDBLabel::updatePixmap() {
/*!
Whatever has changed in KexiDBLabel,
every parameter is set to our private-label.
Just in case...
*/
d->internalLabel->setText( text() );
d->internalLabel->setFixedSize( size() );
d->internalLabel->setPalette( palette() );
d->internalLabel->setAlignment( alignment() );
// d->shadowPixmap = KPixmap(); //parallel repaints won't hurt us cause incomplete pixmap
KPixmap shadowPixmap = d->internalLabel->getShadowPixmap();
if (shadowPixmap.isNull())
return;
d->shadowPixmap = shadowPixmap;
d->shadowPosition = d->internalLabel->m_shadowRect.topLeft();
d->pixmapDirty = false;
repaint();
}
void KexiDBLabel::paintEvent( QPaintEvent* e )
{
QPainter p( this );
if ( d->shadowEnabled ) {
/*!
If required, update the pixmap-cache.
*/
if ( d->pixmapDirty ) {
updatePixmapLater();
}
/*!
If the part that should be redrawn intersects with our shadow,
redraw the shadow where it intersects with e->rect().
Have to move the clipping rect around a bit because
the shadow has to be drawn using an offset relative to
the widgets border.
*/
if ( !d->pixmapDirty && e->rect().contains( d->shadowPosition ) && !d->shadowPixmap.isNull()) {
QRect clipRect = QRect(
QMAX( e->rect().x() - d->shadowPosition.x(), 0 ),
QMAX( e->rect().y() - d->shadowPosition.y(), 0 ),
QMIN( e->rect().width() + d->shadowPosition.x(), d->shadowPixmap.width() ),
QMIN( e->rect().height() + d->shadowPosition.y(), d->shadowPixmap.height() ) );
p.drawPixmap( d->internalLabel->m_shadowRect.topLeft(), d->shadowPixmap, clipRect );
}
}
KexiDBTextWidgetInterface::paint( this, &p, text().isEmpty(), alignment(), false );
QLabel::paintEvent( e );
}
void KexiDBLabel::setValueInternal( const QVariant& add, bool removeOld ) {
if (removeOld)
setText(add.toString());
else
setText( m_origValue.toString() + add.toString() );
}
QVariant KexiDBLabel::value() {
return text();
}
void KexiDBLabel::setInvalidState( const QString& displayText )
{
setText( displayText );
}
bool KexiDBLabel::valueIsNull()
{
return text().isNull();
}
bool KexiDBLabel::valueIsEmpty()
{
return text().isEmpty();
}
bool KexiDBLabel::isReadOnly() const
{
return true;
}
void KexiDBLabel::setReadOnly( bool readOnly )
{
Q_UNUSED(readOnly);
}
QWidget* KexiDBLabel::widget()
{
return this;
}
bool KexiDBLabel::cursorAtStart()
{
return false;
}
bool KexiDBLabel::cursorAtEnd()
{
return false;
}
void KexiDBLabel::clear()
{
setText(QString::null);
}
bool KexiDBLabel::setProperty( const char * name, const QVariant & value )
{
const bool ret = QLabel::setProperty(name, value);
if (d->shadowEnabled) {
if (0==qstrcmp("indent", name) || 0==qstrcmp("font", name) || 0==qstrcmp("margin", name)
|| 0==qstrcmp("frameShadow", name) || 0==qstrcmp("frameShape", name)
|| 0==qstrcmp("frameStyle", name) || 0==qstrcmp("midLineWidth", name)
|| 0==qstrcmp("lineWidth", name)) {
d->internalLabel->setProperty(name, value);
updatePixmap();
}
}
return ret;
}
void KexiDBLabel::setColumnInfo(KexiDB::QueryColumnInfo* cinfo)
{
KexiFormDataItemInterface::setColumnInfo(cinfo);
KexiDBTextWidgetInterface::setColumnInfo(cinfo, this);
}
void KexiDBLabel::setShadowEnabled( bool state ) {
d->shadowEnabled = state;
d->pixmapDirty = true;
if (state)
d->internalLabel->updateFrame();
repaint();
}
void KexiDBLabel::resizeEvent( QResizeEvent* e ) {
if (isVisible())
d->resizeEvent = true;
d->pixmapDirty = true;
QLabel::resizeEvent( e );
}
void KexiDBLabel::fontChange( const QFont& font ) {
d->pixmapDirty = true;
d->internalLabel->setFont( font );
QLabel::fontChange( font );
}
void KexiDBLabel::styleChange( QStyle& style ) {
d->pixmapDirty = true;
QLabel::styleChange( style );
}
void KexiDBLabel::enabledChange( bool enabled ) {
d->pixmapDirty = true;
d->internalLabel->setEnabled( enabled );
QLabel::enabledChange( enabled );
}
void KexiDBLabel::paletteChange( const QPalette& oldPal ) {
Q_UNUSED(oldPal);
d->pixmapDirty = true;
d->internalLabel->setPalette( palette() );
}
/*const QColor & KexiDBLabel::paletteForegroundColor () const
{
return d->foregroundColor;
}
void KexiDBLabel::setPaletteForegroundColor ( const QColor& color )
{
d->foregroundColor = color;
}*/
void KexiDBLabel::frameChanged() {
d->pixmapDirty = true;
d->internalLabel->updateFrame();
QFrame::frameChanged();
}
void KexiDBLabel::showEvent( QShowEvent* e ) {
d->pixmapDirty = true;
QLabel::showEvent( e );
}
void KexiDBLabel::setText( const QString& text ) {
d->pixmapDirty = true;
QLabel::setText( text );
//This is necessary for KexiFormDataItemInterface
valueChanged();
repaint();
}
bool KexiDBLabel::shadowEnabled() const
{
return d->shadowEnabled;
}
#define ClassName KexiDBLabel
#define SuperClassName QLabel
#include "kexiframeutils_p.cpp"
#include "kexidblabel.moc"
|