Public feed #890842

Untitled

public text v1 · immutable
Raw Edit
·published 2008-02-04 03:35 UTC
rendered paste body
/***************************************************************************
 *   Copyright (C) 2005,2006,2007 by Siraj Razick                          *
 *   siraj@kdemail.net                                                     *
 *                                                                         *
 *   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 "ciavc.h"

#include <math.h>

#include <QGraphicsScene>
#include <QPainter>
#include <QStyleOptionGraphicsItem>
#include <QCheckBox>
#include <QPushButton>
#include <QLabel>
#include <QString>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <KDialog>
#include <KLineEdit>
#include <KDebug>
#include <KLocale>
#include <KIcon>
#include <KSharedConfig>

#include <plasma/svg.h>
#include <plasma/layouts/vboxlayout.h>
#include <plasma/widgets/lineedit.h>
#include <plasma/theme.h>

CiaVc::CiaVc(QObject *parent, const QVariantList &args)
    : Plasma::Applet(parent, args), //"plasma-clock-default", appletId)
      m_layout(0)
{
    //setHasConfigurationInterface(true);
}

void CiaVc::init()
{
    setHasConfigurationInterface(true);
    m_layout = new Plasma::VBoxLayout(this);
    m_text = new Plasma::LineEdit(this);
    m_text->setStyled(false);
    m_layout->setGeometry(QRectF(0, 0, 600, 300));
    m_layout->setMargin(0);
 //   m_layout->setSpacing(0);
    m_layout->addItem(m_text);
    m_text->setHtml(i18n("Please wait, getting CIA commit information..."));
    m_text->setTextWidth(-1);
    m_text->update();
    updateGeometry();

    KConfigGroup cg = globalConfig();

    if (cg.hasKey("project")) {
        m_project = cg.readEntry("project");
    } else {
        m_project = QString( "KDE" );
        cg.writeEntry("project", "KDE");
    }

    //TODO: make configurable
    m_engine = dataEngine("ciavc");
    sourceAdded(m_project);
    connect(m_engine, SIGNAL(newSource(QString)), this, SLOT(sourceAdded(QString)));
    connect(m_engine, SIGNAL(sourceRemoved(QString)), this, SLOT(sourceRemoved(QString)));
}

CiaVc::~CiaVc()
{
    delete m_dialog;
    delete m_layout;
}

void CiaVc::constraintsUpdated(Plasma::Constraints constraints)
{
    if (constraints & Plasma::SizeConstraint) {
        prepareGeometryChange();
        m_text->setTextWidth(contentSize().width());
        m_text->update();
        updateGeometry();
        return;
    }
}

QSizeF CiaVc::contentSizeHint() const
{
    if (m_layout) {
        return m_layout->geometry().size();
    }
    return QSizeF(600, 300);
}

void CiaVc::sourceAdded(const QString& source)
{
    m_engine->connectSource(source, this);
    m_text->setDefaultTextColor(Plasma::Theme::self()->textColor());
    m_text->setTextInteractionFlags(Qt::TextSelectableByMouse|Qt::TextSelectableByKeyboard);
    m_text->setObjectName(source);
    kDebug() << "added " << m_text->objectName() << " with expandings " << m_text->expandingDirections();
}

void CiaVc::sourceRemoved(const QString& source)
{
    foreach (QGraphicsItem * child, QGraphicsItem::children()) {
        Plasma::LineEdit * text = dynamic_cast<Plasma::LineEdit*>(child);
        if (!text) {
            continue;
        }

        if (text->objectName() == source) {
            delete text;
        }
        return;
    }
}

bool CiaVc::revisionLessThan(const QString &s1, const QString &s2)
{
    static QRegExp re("::(?:.*)r<b>([0-9]+)");
    int r1 = 0;
    int r2 = 0;

    if (re.indexIn(s1) != -1) {
        r1 = re.cap(1).toInt();
    }
    if (re.indexIn(s2) != -1) {
        r2 = re.cap(1).toInt();
    }
    return r1 > r2;
}

void CiaVc::dataUpdated(const QString &source, const Plasma::DataEngine::Data &data)
{
    Q_UNUSED(source)
    QStringList htmlCommitList;
    QString htmlContent;

    foreach (QVariant item, data) {
       htmlCommitList += item.toString();
    }
    qSort(htmlCommitList.begin(), htmlCommitList.end(), revisionLessThan);

    htmlContent = htmlCommitList.join("");
    // Set some nicer colours.
    htmlContent.replace("#888", "#fff");
    htmlContent.replace("#DDD", "#2f2f2f");
    m_text->setHtml(htmlContent);
    m_text->setTextWidth(size().width()-33);
    m_text->update();
    updateGeometry();
    update();
}
void CiaVc::showConfigurationInterface()
{
    //TODO: implement configuration of projects
    KConfigGroup cg = globalConfig();
    if ( m_dialog == 0 ) {
        m_dialog = new KDialog;
        m_dialog->setButtons( KDialog::Ok | KDialog::Cancel | KDialog::Apply );
        m_dialog->setPlainCaption(i18n("Configure Cia.vc Applet"));

        connect( m_dialog, SIGNAL(okClicked()), this, SLOT(acceptedConfigDialog()));
        connect( m_dialog, SIGNAL(applyClicked()), this, SLOT(acceptedConfigDialog()));

        QWidget *dialogWidget = new QWidget(m_dialog);

        m_projectEdit = new KLineEdit(dialogWidget);

        QLabel *urlLabel = new QLabel(i18n("URL of the CIA.vc project"), dialogWidget);
        QLabel *prefixLabel = new QLabel("http://cia.vc/stats/project/", dialogWidget);

        QVBoxLayout *mainLayout = new QVBoxLayout( dialogWidget );
        QHBoxLayout *urlLayout = new QHBoxLayout( dialogWidget );

        urlLayout->addWidget(prefixLabel);
        urlLayout->addWidget(m_projectEdit);

        mainLayout->addWidget( urlLabel );
        mainLayout->addLayout( urlLayout );

        m_dialog->setMainWidget( dialogWidget );
    }
    m_project = cg.readEntry("project", "KDE");
    m_projectEdit->setText( m_project );
    m_dialog->show();
}

void CiaVc::acceptedConfigDialog()
{
    KConfigGroup cg = globalConfig();
    //TODO: implement configuration of projects
    if ( m_project != m_projectEdit->text() ) {
        cg.writeEntry("project", m_projectEdit->text() );
        sourceRemoved(m_project);
        m_project = m_projectEdit->text();
        sourceAdded(m_project);
    }
}

void CiaVc::paintInterface(QPainter *p, const QStyleOptionGraphicsItem *option, const QRect &contentsRect)
{
    Q_UNUSED(p);
    Q_UNUSED(option);
    Q_UNUSED(contentsRect);
}

#include "ciavc.moc"