You are not logged in.

#1 2013-01-26 15:22:59

Shark
Member
From: /dev/zero
Registered: 2011-02-28
Posts: 686

Need help in C++

Hello!

I have a KDE and i am playing around with global menu. And that's why i want to have a widget that will display active (focused) application name. I found one: kdeplasma-applets-activeapp . The problem is that this widget doesn't have an option for changin font to bold or italic. So i peek in source code but i don't have a clue about it. Here is activeapp.cpp file:

/***************************************************************************
 *                                                                         *
 *   Copyright (C) 2011 Salva Ardid <jsardid@gmail.com>                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU 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 General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA .        *
 ***************************************************************************/

#include "activeapp.h"

//Qt
#include <QGraphicsLinearLayout>
#include <QTimer>
#include <QPropertyAnimation>
#include <QGraphicsDropShadowEffect>

//KDE
#include <KWindowSystem>
#include <KIconLoader>
#include <kconfigdialog.h>
#include <KIcon>
#include <netwm.h>

//Plasma
#include <Plasma/IconWidget>
#include <Plasma/Label>
#include <Plasma/ItemBackground>
#include <Plasma/View>
#include <Plasma/Theme>

#define ICON_PLUS_NAME "showIconPlusName"
#define ICON_ONLY "showOnlyIcon"
#define NAME_ONLY "showOnlyName"

ActiveApp::ActiveApp(QObject *parent, const QVariantList &args)
    : Plasma::Applet(parent, args),
      m_syncDelay(false),
      m_activeWindow(0),
      m_lastActiveWindow(0),
      m_pendingActiveWindow(0)
{
    setHasConfigurationInterface(true);
    lay = new QGraphicsLinearLayout(Qt::Horizontal, this);
    m_currentTask = new Plasma::IconWidget(this);
    m_currentTaskL = new Plasma::Label(this);
    //m_currentTaskL->setStyleSheet("font: bold; color: #CCC;");
    m_currentTaskL->setWordWrap(false); //Single Line
    m_currentTaskL->setMaximumWidth(120);
}


ActiveApp::~ActiveApp()
{
}

void ActiveApp::init()
{
    configChanged();
    connect(KWindowSystem::self(), SIGNAL(activeWindowChanged(WId)),
            this, SLOT(activeWindowChanged(WId)));
    connect(KWindowSystem::self(), SIGNAL(windowChanged(WId)),
            this, SLOT(windowChanged(WId)));
    connect(KWindowSystem::self(), SIGNAL(windowRemoved(WId)),
            this, SLOT(windowRemoved(WId)));
    QGraphicsLinearLayout *lay = new QGraphicsLinearLayout(Qt::Horizontal, this);
    
    lay->setContentsMargins(10, 0, 5, 0);
    
    lay->setSpacing(15);
    
    if (m_showIconPlusName || m_showOnlyIcon) {
      lay->addItem(m_currentTask);
    }
    
    if (m_showIconPlusName || m_showOnlyName) {
      lay->addItem(m_currentTaskL);
    }
    
    activeWindowChanged(KWindowSystem::activeWindow());
    
    QPropertyAnimation *a1 = new QPropertyAnimation(m_currentTask, "opacity");
    a1->setStartValue(0);
    a1->setEndValue(1);
    a1->start(QAbstractAnimation::DeleteWhenStopped);
      
    QPropertyAnimation *a2 = new QPropertyAnimation(m_currentTaskL, "opacity");
    a2->setStartValue(0);
    a2->setEndValue(1);
    a2->start(QAbstractAnimation::DeleteWhenStopped);
    
    QGraphicsDropShadowEffect* shadow = new QGraphicsDropShadowEffect();
    shadow->setBlurRadius(5);
    shadow->setOffset(QPointF(0, 1));
    shadow->setColor(Plasma::Theme::defaultTheme()->color(Plasma::Theme::BackgroundColor));
    setGraphicsEffect(shadow);
}

void ActiveApp::configChanged()
{
  KConfigGroup cg = config();
  m_showIconPlusName = cg.readEntry(ICON_PLUS_NAME, false);
  m_showOnlyIcon = cg.readEntry(ICON_ONLY, false); 
  m_showOnlyName = cg.readEntry(NAME_ONLY, true); 
}

void ActiveApp::configAccepted()
{
  bool changed = false;
  KConfigGroup cg = config();

  if (m_showIconPlusName != ui.radioButton_iconPlusName->isChecked()) {
    m_showIconPlusName = !m_showIconPlusName;
    cg.writeEntry(ICON_PLUS_NAME, m_showIconPlusName);
    changed = true;
  }

  if (m_showOnlyIcon != ui.radioButton_iconOnly->isChecked()) {
    m_showOnlyIcon = !m_showOnlyIcon;
    cg.writeEntry(ICON_ONLY, m_showOnlyIcon);
    changed = true;
  }

  if (m_showOnlyName != ui.radioButton_nameOnly ->isChecked()) {
    m_showOnlyName = !m_showOnlyName;
    cg.writeEntry(NAME_ONLY, m_showOnlyName);
    changed = true;
  }

  if (changed) {
    checkLayout();
    emit configNeedsSaving();
  }
}

void ActiveApp::createConfigurationInterface(KConfigDialog *parent)
{
  QWidget *widget = new QWidget(parent);
  ui.setupUi(widget);
  parent->addPage(widget, i18n("General"), Applet::icon());
  connect(parent, SIGNAL(applyClicked()), this, SLOT(configAccepted()));
  connect(parent, SIGNAL(okClicked()), this, SLOT(configAccepted()));

  ui.radioButton_iconPlusName->setChecked(m_showIconPlusName);
  ui.radioButton_iconOnly->setChecked(m_showOnlyIcon);
  ui.radioButton_nameOnly->setChecked(m_showOnlyName);
}

void ActiveApp::checkLayout()
{

  m_currentTask->setVisible(m_showOnlyIcon || m_showIconPlusName);
  m_currentTaskL->setVisible(m_showOnlyName || m_showIconPlusName);

  if (m_showOnlyIcon) {
    lay->removeItem(m_currentTaskL);
    lay->removeItem(m_currentTask);
    lay->addItem(m_currentTask);
  }
  
  if (m_showOnlyName) {
    lay->removeItem(m_currentTask);
    lay->removeItem(m_currentTaskL);
    lay->addItem(m_currentTaskL);
  }
  
  if (m_showIconPlusName) {
    lay = static_cast<QGraphicsLinearLayout *>(layout());
    lay->addItem(m_currentTask);
    lay->addItem(m_currentTaskL);
  }
  
}

void ActiveApp::constraintsEvent(Plasma::Constraints constraints)
{

    if (((constraints & Plasma::FormFactorConstraint) ||
        (constraints & Plasma::SizeConstraint))) {
        QFontMetrics fm(Plasma::Theme::defaultTheme()->font(Plasma::Theme::DefaultFont));
        if (formFactor() == Plasma::Vertical) {
            m_currentTask->setOrientation(Qt::Vertical);
        } else {
            m_currentTask->setOrientation(Qt::Horizontal);
        }
    }
}

void ActiveApp::windowChanged(WId id)
{
    bool applicationActive = false;

    foreach (QWidget *widget, QApplication::topLevelWidgets()) {
         if (widget->winId() == id) {
             applicationActive = true;
             break;
         }
    }
    if (!applicationActive && id == m_activeWindow) {
        m_pendingActiveWindow = m_activeWindow;
        syncActiveWindow();
    }
}

void ActiveApp::activeWindowChanged(WId id)
{
    m_pendingActiveWindow = id;
    //delay the switch to permit to pass the close action to the proper window if our view accepts focus
    if (!m_syncDelay) {
       syncActiveWindow();
    }
}

void ActiveApp::windowRemoved(WId id)
{
    Q_UNUSED(id)

    QTimer::singleShot(100, this, SLOT(syncActiveWindow()));
}

void ActiveApp::syncActiveWindow()
{
    m_syncDelay = false;
    bool applicationActive = false;

    foreach (QWidget *widget, QApplication::topLevelWidgets()) {
         if (widget->winId() == m_pendingActiveWindow ||
             widget->winId() == KWindowSystem::activeWindow()) {
             applicationActive = true;
             break;
         }
     }

    if (applicationActive && m_pendingActiveWindow > 0) {
        m_activeWindow = 0;
        
        if (m_showOnlyIcon || m_showIconPlusName) {
          m_currentTask->setIcon("preferences-system-windows");
        }
        
        m_currentTaskL->setText("");
        
        if (m_showOnlyName || m_showIconPlusName) {
          lay = static_cast<QGraphicsLinearLayout *>(layout());
          lay->removeItem(m_currentTaskL);
          lay->removeItem(m_currentTask);
          lay->setContentsMargins(0, 0, 0, 0);
        }
        
        if (m_showOnlyIcon || m_showIconPlusName) {
          lay = static_cast<QGraphicsLinearLayout *>(layout());
          lay->setContentsMargins(10, 0, 5, 0);
          lay->addItem(m_currentTask);
        }
        
        QPropertyAnimation *a1 = new QPropertyAnimation(m_currentTask, "opacity");
        a1->setStartValue(0);
        a1->setEndValue(1);
        a1->start(QAbstractAnimation::DeleteWhenStopped);
        
    } else if (m_pendingActiveWindow > 0){ 
        lay = static_cast<QGraphicsLinearLayout *>(layout());
        lay->setContentsMargins(10, 0, 5, 0);
      
        QString prevText=m_currentTaskL->text();
        m_activeWindow = m_pendingActiveWindow;
        m_lastActiveWindow = m_pendingActiveWindow;
        
        if (m_showOnlyIcon || m_showIconPlusName) {
          m_currentTask->setIcon(KWindowSystem::icon(m_activeWindow, KIconLoader::SizeSmallMedium, KIconLoader::SizeSmallMedium));
        }
        
        if (m_showOnlyName || m_showIconPlusName) {
          QString tmp,tmp1,tmp2;
          QStringList list;
          
          KWindowInfo info = KWindowSystem::windowInfo(m_activeWindow, NET::WMName|NET::WMState, NET::WM2WindowClass);
        
          tmp1=info.windowClassClass();
          tmp2=info.name();
          if (!tmp1.contains(QRegExp("^\\s*$")) || !tmp2.contains(QRegExp("^\\s*$"))) {
            lay = static_cast<QGraphicsLinearLayout *>(layout());
            if (m_showIconPlusName || m_showOnlyIcon) {
              lay->removeItem(m_currentTask);
              lay->addItem(m_currentTask);
            }
            lay->addItem(m_currentTaskL);
            
            if (tmp1.contains(QRegExp("^\\s*$")) || tmp1.contains(QRegExp("\\–")) || tmp1.contains(QRegExp("\\.py$")) || tmp1.contains(QRegExp("\\.bin$")) || tmp1.contains(QRegExp("\\-mathworks\\-"))) {
              list = tmp2.split(QRegExp("\\–+"));
              if (list.size()==1) {
                list = list[0].split(QRegExp("[0-9]+"));
                tmp=list[0];
                tmp=tmp.remove(QRegExp("^\\s+"));
                tmp=tmp.remove(QRegExp("\\s+$"));
              }
              else {
                tmp=list[list.size()-1].remove(QRegExp("^\\s+"));
                tmp=list[list.size()-1].remove(QRegExp("\\s+$"));
              }
            }
            else if (tmp1.contains(QRegExp("^Software-center$"))){
              tmp="Software-center";
            } 
            else {
              list = tmp1.split(QRegExp("\\-+"));
              tmp=list[0];
            }
            if (tmp.contains(QRegExp("^libreoffice$"))){
              tmp="LibreOffice";
            } 
            m_currentTaskL->setText(tmp);
          }
        }
        
        if (QString::compare(m_currentTaskL->text(),prevText)) {
            QPropertyAnimation *a1 = new QPropertyAnimation(m_currentTask, "opacity");
            a1->setStartValue(0);
            a1->setEndValue(1);
            a1->start(QAbstractAnimation::DeleteWhenStopped);
    
            QPropertyAnimation *a2 = new QPropertyAnimation(m_currentTaskL, "opacity");
            a2->setStartValue(0);
            a2->setEndValue(1);
            a2->start(QAbstractAnimation::DeleteWhenStopped);
        }
    }

    m_pendingActiveWindow = 0;
}

void ActiveApp::setSyncDelay(bool delay)
{
    m_syncDelay = delay;
}

void ActiveApp::windowItemClicked()
{
    if (sender() && m_windowIcons.contains(static_cast<Plasma::IconWidget*>(sender()))) {
        KWindowSystem::forceActiveWindow(m_windowIcons.value(static_cast<Plasma::IconWidget*>(sender())));
    }
}

#include "activeapp.moc"

Can anybody help with the bold issue? Thanks!


If you have built castles in the air, your work need not be lost; that is where they should be. Now put foundations under them.
Henry David Thoreau

Registered Linux User: #559057

Offline

#2 2013-01-26 20:57:17

mrunion
Member
From: Jonesborough, TN
Registered: 2007-01-26
Posts: 1,938
Website

Re: Need help in C++

Seems to me (though I cannot be sure) that the commented out line:

//m_currentTaskL->setStyleSheet("font: bold; color: #CCC;");

is the answer, right? Is that code you already tried to uncomment and test?

Last edited by mrunion (2013-01-26 20:57:42)


Matt

"It is very difficult to educate the educated."

Offline

#3 2013-01-26 21:37:46

Shark
Member
From: /dev/zero
Registered: 2011-02-28
Posts: 686

Re: Need help in C++

mrunion wrote:

Seems to me (though I cannot be sure) that the commented out line:

//m_currentTaskL->setStyleSheet("font: bold; color: #CCC;");

is the answer, right? Is that code you already tried to uncomment and test?

Hi!

I tried that with no luck. Any further suggestions?


If you have built castles in the air, your work need not be lost; that is where they should be. Now put foundations under them.
Henry David Thoreau

Registered Linux User: #559057

Offline

#4 2013-01-27 03:43:35

Stebalien
Member
Registered: 2010-04-27
Posts: 1,239
Website

Re: Need help in C++

What about this:

m_currentTaskL->setStyleSheet("* { font-weight: bold; }");

Steven [ web : git ]
GPG:  327B 20CE 21EA 68CF A7748675 7C92 3221 5899 410C

Offline

#5 2013-01-27 08:10:46

Shark
Member
From: /dev/zero
Registered: 2011-02-28
Posts: 686

Re: Need help in C++

Stebalien wrote:

What about this:

m_currentTaskL->setStyleSheet("* { font-weight: bold; }");

Hi!

No, that didn't help. Any other suggestion?

Thanks!


If you have built castles in the air, your work need not be lost; that is where they should be. Now put foundations under them.
Henry David Thoreau

Registered Linux User: #559057

Offline

#6 2013-01-27 12:04:30

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,554
Website

Re: Need help in C++

Have you looked at the code or documentation for Plasma::Label?  That's where you should be looking.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

Board footer

Powered by FluxBB