From kde-commits Wed Oct 31 22:36:57 2012 From: Emmanuel Lepage Vallee Date: Wed, 31 Oct 2012 22:36:57 +0000 To: kde-commits Subject: [sflphone-kde/october2012_ui] src: [ #16349 ] Refactor the loadermanager so the whole loading part w Message-Id: <20121031223657.C9115A6078 () git ! kde ! org> X-MARC-Message: https://marc.info/?l=kde-commits&m=135172304131152 Git commit ffe9bf3e6ee189699009794b6b61dac0932e0491 by Emmanuel Lepage Vall= ee. Committed on 31/10/2012 at 23:36. Pushed by lepagevalleeemmanuel into branch 'october2012_ui'. [ #16349 ] Refactor the loadermanager so the whole loading part will be par= t of the tip. Tips are not SVG based anymore, but QPainter based M +1 -1 src/CMakeLists.txt M +4 -4 src/callview.cpp M +2 -2 src/callview.h D +0 -103 src/icons/tips/dial_with_dialpad.svg A +65 -0 src/icons/tips/downArrow.svg A +67 -0 src/icons/tips/keyboard.svg M +1 -1 src/klib/CMakeLists.txt M +75 -37 src/klib/tip.cpp M +17 -3 src/klib/tip.h M +14 -6 src/klib/tipanimationwrapper.cpp M +4 -4 src/klib/tipanimationwrapper.h R +17 -50 src/klib/tipmanager.cpp [from: src/klib/svgtiploader.cpp - 0= 69% similarity] R +8 -8 src/klib/tipmanager.h [from: src/klib/svgtiploader.h - 085% = similarity] http://commits.kde.org/sflphone-kde/ffe9bf3e6ee189699009794b6b61dac0932e0491 diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b5f0d59..62e319e 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -123,4 +123,4 @@ INSTALL( FILES icons/hold_grayscale.png DESTIN= ATION ${DATA_INSTALL_DIR INSTALL( FILES icons/unhold_grayscale.png DESTINATION ${DATA_INSTA= LL_DIR}/sflphone-client-kde ) INSTALL( FILES icons/refuse_grayscale.png DESTINATION ${DATA_INSTA= LL_DIR}/sflphone-client-kde ) INSTALL( FILES icons/pickup_grayscale.png DESTINATION ${DATA_INSTA= LL_DIR}/sflphone-client-kde ) -INSTALL( FILES icons/tips/dial_with_dialpad.svg DESTINATION ${DATA_INSTA= LL_DIR}/sflphone-client-kde/tips ) +INSTALL( FILES icons/tips/keyboard.svg DESTINATION ${DATA_INSTA= LL_DIR}/sflphone-client-kde/tips ) diff --git a/src/callview.cpp b/src/callview.cpp index d32d807..a64f31d 100644 --- a/src/callview.cpp +++ b/src/callview.cpp @@ -50,7 +50,7 @@ #include "sflphoneaccessibility.h" #include "widgets/conferencebox.h" #include "widgets/callviewoverlaytoolbar.h" -#include "klib/svgtiploader.h" +#include "klib/tipmanager.h" = ///CallTreeItemDelegate: Delegates for CallTreeItem class CallTreeItemDelegate : public QStyledItemDelegate @@ -275,10 +275,10 @@ CallView::CallView(QWidget* parent) : QTreeWidget(par= ent),m_pActiveOverlay(0),m_ QGridLayout* gl =3D new QGridLayout ( m_pTransferOverlay ); QLabel* lblImg =3D new QLabel ( image ); m_pCanvasToolbar =3D new CallViewOverlayToolbar(this); - m_pTip =3D new SvgTipLoader(this, + m_pTip =3D new TipManager(this, KStandardDirs::locate("data", - "sflphone-client-kde/tips/dial_w= ith_dialpad.svg"), - i18n("Use the dialpad below or s= tart typing a number. "),4); + "sflphone-client-kde/tips/keyboa= rd.svg"), + i18n("Use the dialpad below or s= tart typing a number. Use the dialpad below or start typing a number. Use t= he dsadasdialpad below or start typing a number. Use thasdasde dialpad belo= w or sasdasdtart typing a number. "),4); = m_pTransferOverlay->setVisible(false); m_pTransferOverlay->resize(size()); diff --git a/src/callview.h b/src/callview.h index 46bffd2..833fb27 100644 --- a/src/callview.h +++ b/src/callview.h @@ -38,7 +38,7 @@ class KLineEdit; class CallTreeItem; class CallTreeItemDelegate; class CallViewOverlayToolbar; -class SvgTipLoader; +class TipManager; = //Typedef typedef CallModel TreeWidgetCallModel; @@ -116,7 +116,7 @@ class CallView : public QTreeWidget { CallViewOverlay* m_pActiveOverlay ; Call* m_pCallPendingTransfer; CallViewOverlayToolbar* m_pCanvasToolbar ; - SvgTipLoader* m_pTip ; + TipManager* m_pTip ; = protected: //Reimlementation diff --git a/src/icons/tips/dial_with_dialpad.svg b/src/icons/tips/dial_wit= h_dialpad.svg deleted file mode 100644 index 8f8334d..0000000 --- a/src/icons/tips/dial_with_dialpad.svg +++ /dev/null @@ -1,103 +0,0 @@ - - - - - - - - - - image/svg+xml - - - - - - - - - - - - - diff --git a/src/icons/tips/downArrow.svg b/src/icons/tips/downArrow.svg new file mode 100644 index 0000000..823a514 --- /dev/null +++ b/src/icons/tips/downArrow.svg @@ -0,0 +1,65 @@ + + + + + + + + + + image/svg+xml + + + + + + + + + diff --git a/src/icons/tips/keyboard.svg b/src/icons/tips/keyboard.svg new file mode 100644 index 0000000..c566b80 --- /dev/null +++ b/src/icons/tips/keyboard.svg @@ -0,0 +1,67 @@ + + + + + + + + image/svg+xml + + + + + + + + + + + diff --git a/src/klib/CMakeLists.txt b/src/klib/CMakeLists.txt index ee3361b..bac9ef8 100644 --- a/src/klib/CMakeLists.txt +++ b/src/klib/CMakeLists.txt @@ -32,7 +32,7 @@ set( ksflphone_LIB_SRCS akonadibackend.cpp sortabledockcommon.cpp configurationskeleton.cpp - svgtiploader.cpp + tipmanager.cpp tip.cpp tipanimationwrapper.cpp ) diff --git a/src/klib/tip.cpp b/src/klib/tip.cpp index 691572b..c714e9f 100644 --- a/src/klib/tip.cpp +++ b/src/klib/tip.cpp @@ -20,44 +20,26 @@ //Qt #include #include +#include #include = //KDE #include = //SFLPhone -#include "svgtiploader.h" +#include "tipmanager.h" = ///Constructor Tip::Tip(QWidget* parent,const QString& path, const QString& text, int max= Line) : QObject(parent),m_OriginalText(text),m_MaxLine(maxLine),m_Position(= TipPosition::Bottom),m_IsMaxSize(false),m_pR(nullptr), -m_OriginalPalette(parent->palette()),m_AnimationIn(TipAnimation::Translati= onTop),m_AnimationOut(TipAnimation::TranslationTop) +m_OriginalPalette(parent->palette()),m_AnimationIn(TipAnimation::Translati= onTop),m_AnimationOut(TipAnimation::TranslationTop),m_pFont(nullptr) { - QFile file(path); - if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { - kDebug() << "The tip" << path << "failed to load: No such file"; - } - else { - m_OriginalFile =3D file.readAll(); - m_OriginalFile.replace("BACKGROUD_COLOR_ROLE",brightOrDarkBase()?"#0= 00000":"#ffffff"); - int lastIndexOf =3D -1; - QStringList lines =3D SvgTipLoader::stringToLineArray(parent->font()= ,m_OriginalText,250); - for (int i=3D(maxLine < lines.size())?maxLine:lines.size();i;i--) { - m_OriginalFile.replace("BASE_ROLE_COLOR",m_OriginalPalette.base()= .color().name().toAscii()); - int idx =3D m_OriginalFile.lastIndexOf(QString("TLline" + QString= ::number(i)).toAscii(), lastIndexOf); - if (idx > 0) { - lastIndexOf =3D idx; - if (i=3D=3DmaxLine && maxLine < lines.size()) - lines[i-1] +=3D "..."; - m_OriginalFile.insert(idx+9,lines[i-1].toAscii()); - } - } - } + loadSvg(path); } = ///Destructor Tip::~Tip() { - = + if (m_pFont) delete m_pFont; } = /** @@ -68,35 +50,91 @@ QSize Tip::reload(const QRect& availableSize) { if (m_CurrentRect !=3D availableSize && !(m_IsMaxSize && m_CurrentSize.= width()*1.25 < availableSize.width())) { m_CurrentRect =3D availableSize; - int wwidth(availableSize.width()),wheight((availableSize.width())*0.= 539723102); + m_CurrentRect.setHeight(PADDING); = - if (wheight > 170) { - wheight =3D 170; - wwidth =3D wheight*1.85280192; - m_IsMaxSize =3D true; - } - else { - m_IsMaxSize =3D false; + //One 1000px wide line is not so useful, this may change later (vari= able) + if (m_CurrentRect.width() > MAX_WIDTH) { + m_CurrentRect.setWidth( MAX_WIDTH ); } - m_CurrentImage =3D QImage(QSize(wwidth,wheight),QImage::Format_RGB88= 8); + m_CurrentRect.setWidth(m_CurrentRect.width()); + + //Get area required to display the text + QRect textRect =3D getTextRect(m_OriginalText); + m_CurrentRect.setHeight(m_CurrentRect.height() + textRect.height() += PADDING + getDecorationRect().height()); + + //Create the background image + m_CurrentImage =3D QImage(QSize(m_CurrentRect.width(),m_CurrentRect.= height()),QImage::Format_RGB888); m_CurrentImage.fill(m_OriginalPalette.base().color() ); QPainter p(&m_CurrentImage); + p.setRenderHint(QPainter::Antialiasing, true); + p.setFont(font()); + = - if (!m_pR) - m_pR =3D new QSvgRenderer(m_OriginalFile); + //Draw the tip rectangle + p.setPen(QPen(m_OriginalPalette.base().color())); + p.setBrush(QBrush(brightOrDarkBase()?Qt::black:Qt::white)); + p.drawRoundedRect(QRect(0,0,m_CurrentRect.width(),m_CurrentRect.heig= ht()),10,10); = - m_CurrentSize =3D QSize(wwidth,wheight); + //Draw the wrapped text in textRectS + p.drawText(textRect,Qt::TextWordWrap|Qt::AlignJustify,m_OriginalText= ); = - if (availableSize.height() >=3D wheight) - m_pR->render(&p,QRect(0,0,wwidth,wheight)); + + //If the widget is subclassed, this would allow decorations to be ad= ded like images + paintDecorations(p,textRect); + = + //Set the size from the RECT //TODO redundant + m_CurrentSize =3D QSize(m_CurrentRect.width(),m_CurrentRect.height()= ); } return m_CurrentSize; } = +QRect Tip::getTextRect(const QString& text) +{ + QFontMetrics metric(font()); + QRect rect =3D metric.boundingRect(QRect(PADDING,PADDING,m_CurrentRect.= width()-2*PADDING,999999),Qt::AlignJustify|Qt::TextWordWrap,text); + return rect; +} + ///Check if the thene color scheme is darker than #888888 ///@return true =3D bright, false =3D dark bool Tip::brightOrDarkBase() { QColor color =3D m_OriginalPalette.base().color(); return (color.red() > 128 && color.green() > 128 && color.blue() > 128); +} + + +QRect Tip::getDecorationRect() +{ + return QRect(0,0,m_CurrentSize.width()-2*PADDING,60); +} + +void Tip::paintDecorations(QPainter& p, const QRect& textRect) +{ + if (!m_pR) + m_pR =3D new QSvgRenderer(m_OriginalFile); + m_pR->render(&p,QRect(m_CurrentRect.width() - PADDING - 50*2.5914332784= 2 - 10 ,textRect.y()+textRect.height() + 10,50*2.59143327842,50)); +} + +const QFont& Tip::font() +{ + if (!m_pFont) { + m_pFont =3D new QFont(); + m_pFont->setBold(true); + } + return (const QFont&) *m_pFont; +} + +QString Tip::loadSvg(const QString& path) +{ + QFile file(path); + if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { + kDebug() << "The tip" << path << "failed to load: No such file"; + } + else { + m_OriginalFile =3D file.readAll(); + m_OriginalFile.replace("BACKGROUD_COLOR_ROLE",brightOrDarkBase()?"#0= 00000":"#ffffff"); + m_OriginalFile.replace("BASE_ROLE_COLOR",m_OriginalPalette.base().co= lor().name().toAscii()); + } + return m_OriginalFile; } \ No newline at end of file diff --git a/src/klib/tip.h b/src/klib/tip.h index b49a5a2..648c232 100644 --- a/src/klib/tip.h +++ b/src/klib/tip.h @@ -32,12 +32,15 @@ class QSvgRenderer; //SFLPhone #include "../lib/typedefs.h" = +//Qt +class QPainter; + ///A tip to be passed to the TipLoader class Tip : public QObject { Q_OBJECT public: - friend class SvgTipLoader; + friend class TipManager; friend class TipAnimationWrapper; Tip(QWidget* parent =3D nullptr,const QString& path=3D"", const QString= & text=3D"", int maxLine=3D4); virtual ~Tip(); @@ -48,7 +51,7 @@ public: Middle, Bottom }; - = + enum TipAnimation { Fade, TranslationTop, @@ -57,7 +60,7 @@ public: TranslationRight, None }; - = + //Mutator QSize reload(const QRect& availableSize); = @@ -76,9 +79,20 @@ protected: QPalette m_OriginalPalette; TipAnimation m_AnimationIn ; TipAnimation m_AnimationOut ; + QFont* m_pFont ; + + static const int PADDING =3D 15; + static const int MAX_WIDTH =3D 350; = //Helper bool brightOrDarkBase(); + QString loadSvg(const QString& path); + + //To reimplement if needed + virtual QRect getTextRect(const QString& text); + virtual QRect getDecorationRect(); + virtual void paintDecorations(QPainter& p, const QRect& textRect); + virtual const QFont& font(); }; = #endif \ No newline at end of file diff --git a/src/klib/tipanimationwrapper.cpp b/src/klib/tipanimationwrappe= r.cpp index e3a8c20..607d95a 100644 --- a/src/klib/tipanimationwrapper.cpp +++ b/src/klib/tipanimationwrapper.cpp @@ -23,11 +23,11 @@ = //SFLPhone #include "tip.h" -#include "svgtiploader.h" +#include "tipmanager.h" = -TipAnimationWrapper::TipAnimationWrapper(Tip* aTip, SvgTipLoader* parent) = : QObject(parent),m_MaxStep(15),m_Step(0),m_pTimer(nullptr),m_pTip(aTip),m_= TipSize(QSize(0,0)) +TipAnimationWrapper::TipAnimationWrapper(Tip* aTip, TipManager* parent) : = QObject(parent),m_MaxStep(15),m_Step(0),m_pTimer(nullptr),m_pTip(aTip),m_Ti= pSize(QSize(0,0)) { - connect(parent,SIGNAL(sizeChanged(QRect)),this,SLOT(sizeChanged(QRect))= ); + connect(parent,SIGNAL(sizeChanged(QRect,bool)),this,SLOT(sizeChanged(QR= ect,bool))); } = TipAnimationWrapper::~TipAnimationWrapper() @@ -35,13 +35,20 @@ TipAnimationWrapper::~TipAnimationWrapper() = } = -void TipAnimationWrapper::sizeChanged(QRect rect) +void TipAnimationWrapper::sizeChanged(QRect rect,bool ignoreAnim) { if (m_pTip) { m_TipSize =3D m_pTip->reload(QRect(0,0,rect.width(),rect.height())); m_CurrentImage =3D m_pTip->m_CurrentImage; } m_ParentRect =3D rect; + if (!ignoreAnim) { + Tip::TipAnimation anim =3D m_CurrentAnimation; + m_CurrentAnimation =3D Tip::None; + m_Step =3D 0; + step(); + m_CurrentAnimation =3D anim; + } } = const QImage& TipAnimationWrapper::currentImage() @@ -79,9 +86,10 @@ void TipAnimationWrapper::start(bool show) void TipAnimationWrapper::step() { m_Step++; - if (m_Step > m_MaxStep && m_pTimer) { + if (m_Step > m_MaxStep) { m_Step =3D 0; - m_pTimer->stop(); + if (m_pTimer) + m_pTimer->stop(); emit animationEnded(); } else { diff --git a/src/klib/tipanimationwrapper.h b/src/klib/tipanimationwrapper.h index a006050..a7c2ab3 100644 --- a/src/klib/tipanimationwrapper.h +++ b/src/klib/tipanimationwrapper.h @@ -27,7 +27,7 @@ class QTimer; = //SFLPhone #include "tip.h" -class SvgTipLoader; +class TipManager; = //Structs struct FrameDescription { @@ -40,12 +40,12 @@ class TipAnimationWrapper : public QObject { Q_OBJECT public: - TipAnimationWrapper(Tip* aTip, SvgTipLoader* parent); + TipAnimationWrapper(Tip* aTip, TipManager* parent); virtual ~TipAnimationWrapper(); = //Mutator void start(bool show =3D true); - = + //Getter const QImage& currentImage(); QSize tipSize(); @@ -68,7 +68,7 @@ protected: = private slots: void step(); - void sizeChanged(QRect rect); + void sizeChanged(QRect rect,bool ignoreAnim); = signals: void animationStep(FrameDescription desc); diff --git a/src/klib/svgtiploader.cpp b/src/klib/tipmanager.cpp similarity index 69% rename from src/klib/svgtiploader.cpp rename to src/klib/tipmanager.cpp index 71da381..b034c2e 100644 --- a/src/klib/svgtiploader.cpp +++ b/src/klib/tipmanager.cpp @@ -15,7 +15,7 @@ * You should have received a copy of the GNU General Public License = * * along with this program. If not, see .= * *************************************************************************= **/ -#include "svgtiploader.h" +#include "tipmanager.h" = //Qt #include @@ -37,7 +37,7 @@ bool ResizeEventFilter::eventFilter(QObject *obj, QEvent = *event) } = ///Constructor -SvgTipLoader::SvgTipLoader(QTreeView* parent, const QString& path, const Q= String& text, int maxLine):QObject(parent), +TipManager::TipManager(QTreeView* parent, const QString& path, const QStri= ng& text, int maxLine):QObject(parent), m_OriginalPalette(parent->palette()),m_pParent(parent),m_BottomMargin(0),m= _TopMargin(0),m_Tip(parent,path,text,maxLine), m_pAnim(&m_Tip,this) { @@ -49,13 +49,13 @@ m_pAnim(&m_Tip,this) } = ///Get the current image -QImage SvgTipLoader::getImage() +QImage TipManager::getImage() { return m_CurrentImage; } = ///Reload the SVG with new size -void SvgTipLoader::reload() +void TipManager::reload() { int width(m_pParent->width()),height(m_pParent->height()); int effectiveHeight =3D height-m_BottomMargin-m_TopMargin; @@ -74,77 +74,44 @@ void SvgTipLoader::reload() m_pParent->setPalette(p2); } = -/** - * Take a long string and manually wrap it using a specific font. This is = needed because SVG - * does not natively support wrapping and Qt does not implement the few ha= cks around this - * so it is better to create a field for each line - */ -QStringList SvgTipLoader::stringToLineArray(const QFont& font, QString tex= t, int width ) -{ - QFontMetrics metric(font); - int total =3D 0; - QStringList result; - QStringList words =3D text.split(' '); - - QString tmp; - foreach(QString word, words) { - int mW =3D metric.width(word+' '); - if (mW + total > width){ - result << tmp; - tmp =3D QString(); - total =3D 0; - } - total +=3D mW; - tmp +=3D word + ' '; - } - if (tmp.size()) { - result << tmp; - } - - return result; -} - ///Set the top margin -void SvgTipLoader::setTopMargin(int margin) +void TipManager::setTopMargin(int margin) { bool changed =3D !(m_TopMargin =3D=3D margin); m_TopMargin =3D margin; - if (changed) changeSize(); + if (changed) changeSize(true); } = ///Set the bottom margin -void SvgTipLoader::setBottomMargin(int margin) +void TipManager::setBottomMargin(int margin) { bool changed =3D !(m_BottomMargin =3D=3D margin); m_BottomMargin =3D margin; - if (changed) changeSize(); + if (changed) changeSize(true); } = ///Set the current tip, hide the previous one, if any -void SvgTipLoader::setCurrentTip(bool tip) +void TipManager::setCurrentTip(bool tip) { - m_pCurrentTip =3D tip; - /*if (tip) { + if (tip !=3D m_pCurrentTip) { + m_pCurrentTip =3D tip; + m_pAnim.start(tip); + changeSize(true); } - else { - //m_CurrentFrame =3D {QPoint(0,0),QRect(0,0,0,0),0}; - }*/ - m_pAnim.start(tip); - changeSize(); } = ///Callback for new animation frame -void SvgTipLoader::animationStep(FrameDescription desc) +void TipManager::animationStep(FrameDescription desc) { m_CurrentFrame =3D desc; reload(); } = ///Callback when size change -void SvgTipLoader::changeSize() +void TipManager::changeSize(bool ignoreAnim) { int width(m_pParent->width()),height(m_pParent->height()); int effectiveHeight =3D height-m_BottomMargin-m_TopMargin; - qDebug() << height << (height-effectiveHeight) << effectiveHeight << (w= idth-30); - emit sizeChanged(QRect(15,m_TopMargin,width-30,effectiveHeight)); +// qDebug() << height << (height-effectiveHeight) << effectiveHeight <<= (width-30); + emit sizeChanged(QRect(15,m_TopMargin,width-30,effectiveHeight),ignoreA= nim); } \ No newline at end of file diff --git a/src/klib/svgtiploader.h b/src/klib/tipmanager.h similarity index 85% rename from src/klib/svgtiploader.h rename to src/klib/tipmanager.h index 5c44486..5224818 100644 --- a/src/klib/svgtiploader.h +++ b/src/klib/tipmanager.h @@ -30,30 +30,30 @@ class QSvgRenderer; #include "../lib/typedefs.h" #include "tip.h" #include "tipanimationwrapper.h" -class SvgTipLoader; +class TipManager; = //Classes class ResizeEventFilter : public QObject { Q_OBJECT public: - ResizeEventFilter(SvgTipLoader* parent) : QObject(0) { + ResizeEventFilter(TipManager* parent) : QObject(0) { m_pLoader =3D parent; } protected: bool eventFilter(QObject *obj, QEvent *event); private: - SvgTipLoader* m_pLoader; + TipManager* m_pLoader; }; = ///This class create a background brush for a QWidget with a tip window -class LIB_EXPORT SvgTipLoader : public QObject +class LIB_EXPORT TipManager : public QObject { Q_OBJECT friend class ResizeEventFilter; public: //Constructor - SvgTipLoader(QTreeView* parent, const QString& path, const QString& tex= t, int maxLine); + TipManager(QTreeView* parent, const QString& path, const QString& text,= int maxLine); = //Getter QImage getImage(); @@ -64,7 +64,7 @@ public: void setCurrentTip(bool tip); = //Helper - static QStringList stringToLineArray(const QFont& font, QString text, i= nt width =3D -1); +// static QStringList stringToLineArray(const QFont& font, QString text= , int width =3D -1); = private: //Methods @@ -83,10 +83,10 @@ private: = private slots: void animationStep(FrameDescription desc); - void changeSize(); + void changeSize(bool ignoreAnim =3D false); = signals: - void sizeChanged(QRect newRect); + void sizeChanged(QRect newRect,bool ignoreAnim); = }; =20