Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Copyright (c) 2010, Stijn Leenknegt
- All rights reserved.
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions are met:
- * Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
- * Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in the
- documentation and/or other materials provided with the distribution.
- * Neither the name of the KHL nor the
- names of its contributors may be used to endorse or promote products
- derived from this software without specific prior written permission.
- THIS SOFTWARE IS PROVIDED BY Stijn Leenknegt ''AS IS'' AND ANY
- EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- DISCLAIMED. IN NO EVENT SHALL Stijn Leenknegt BE LIABLE FOR ANY
- DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
- #include <QPainter>
- #include <QFontMetrics>
- #include <QSizeF>
- #include <KLocale>
- #include <QWidget>
- #include <QString>
- #include <QList>
- #include <kconfigdialog.h>
- #include <kconfiggroup.h>
- #include <plasma/theme.h>
- #include <Plasma/Separator>
- #include "entrywidget.h"
- #include "khluurrooster.h"
- KHLUurrooster::KHLUurrooster(QObject *parent, const QVariantList &args)
- : Plasma::Applet(parent, args)
- {
- // this will get us the standard applet background, for free!
- setHasConfigurationInterface(true);
- setBackgroundHints(DefaultBackground);
- resize(300, 200);
- //setup the rooster object
- rooster = new Uurrooster();
- connect(rooster, SIGNAL(errorOccurred(QString)), this, SLOT(roosterError(QString)));
- connect(rooster, SIGNAL(entriesChanged()), this, SLOT(sendNextIcalLink()));
- //set the timer
- timer = new QTimer(this);
- connect(timer, SIGNAL(timeout()), this, SLOT(updateRoosters()));
- //make layout for the widget
- layout = new QGraphicsGridLayout(this);
- setLayout(layout);
- //set date
- date = QDate::currentDate();
- //set colors
- ical_1_color = "black";
- ical_2_color = "blue";
- ical_3_color = "green";
- //set UI components
- headerLabel = new Plasma::Label();
- vorigeButton = new Plasma::PushButton();
- volgendeButton = new Plasma::PushButton();
- m_scrollWidget = new Plasma::ScrollWidget();//this?
- m_scroller = new QGraphicsWidget();
- m_scrollerLayout = new QGraphicsLinearLayout(Qt::Vertical, m_scroller);
- //configre the UI components
- headerLabel->setAlignment(Qt::AlignCenter);
- m_scrollWidget->setMinimumSize(300, 200);
- m_scrollWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
- m_scrollWidget->setWidget(m_scroller);
- //connect the buttons
- connect(vorigeButton, SIGNAL(clicked()), this, SLOT(prevDate()));
- connect(volgendeButton, SIGNAL(clicked()), this, SLOT(nextDate()));
- //add to layout
- layout->addItem(headerLabel, 0, 0, 1, 2);
- layout->addItem(m_scrollWidget, 1, 0, 1, 2);
- layout->addItem(vorigeButton, 2, 0);
- layout->addItem(volgendeButton, 2, 1);
- }
- KHLUurrooster::~KHLUurrooster()
- {
- }
- void KHLUurrooster::init()
- {
- readConfig();
- if(ical_1.isEmpty()) { //no configuration setted yet
- this->setConfigurationRequired(true, "Je moet op zijn minst 1 iCal link opgeven");
- } else {
- configChanged();
- }
- }
- void KHLUurrooster::createConfigurationInterface(KConfigDialog *parent)
- {
- QWidget *widget = new QWidget(0);
- ui.setupUi(widget);
- //als er op ok of apply gedrukt is, moet de config opgeslaan worden
- connect(parent, SIGNAL(okClicked()), this, SLOT(configAccepted()));
- connect(parent, SIGNAL(applyClicked()), this, SLOT(configAccepted()));
- //stel de value van de textbox in
- ui.txtIcal_1->setText(ical_1);
- ui.txtIcal_2->setText(ical_2);
- ui.txtIcal_3->setText(ical_3);
- parent->addPage(widget, "KHL Uurrooster", icon());
- }
- void KHLUurrooster::readConfig()
- {
- KConfigGroup cg = config();
- ical_1 = cg.readEntry("ical_1");
- ical_2 = cg.readEntry("ical_2");
- ical_3 = cg.readEntry("ical_3");
- }
- void KHLUurrooster::configChanged()
- {
- //stop the timer if needed
- if(timer->isActive()) {
- timer->stop();
- }
- //get the config
- readConfig();
- //check if filled in
- if(! ical_1.isEmpty()) { //no configuration setted yet
- this->setConfigurationRequired(false, "Je moet op zijn minst 1 iCal link opgeven");
- }
- //update het uurrooster, hier wordt het ical terug opgehaald.
- ical_busy = 1;
- rooster->removeEntries();
- rooster->update(ical_1, ical_1_color);
- //start the timer with a given timeout, default it's once a hour
- timer->start(60 * 60 * 1000);
- }
- void KHLUurrooster::configAccepted()
- {
- ical_1 = ui.txtIcal_1->text();
- ical_2 = ui.txtIcal_2->text();
- ical_3 = ui.txtIcal_3->text();
- KConfigGroup cg = config();
- cg.writeEntry("ical_1", ical_1);
- cg.writeEntry("ical_2", ical_2);
- cg.writeEntry("ical_3", ical_3);
- emit configNeedsSaving();
- }
- void KHLUurrooster::paintInterface(QPainter *p, const QStyleOptionGraphicsItem *option, const QRect &contentsRect)
- {
- p->setRenderHint(QPainter::SmoothPixmapTransform);
- p->setRenderHint(QPainter::Antialiasing);
- layout->setGeometry(contentsRect);
- }
- void KHLUurrooster::displayEntries()
- {
- //de uurrooster entries, mooi gesorteerd.
- QList<uurrooster_entry> *entries= rooster->getEntries(date);
- //delete body frame entries
- QGraphicsLayoutItem *item;
- while(m_scrollerLayout->count() > 0) {
- item = m_scrollerLayout->itemAt(m_scrollerLayout->count() - 1);
- m_scrollerLayout->removeItem(item);
- delete item;
- }
- //set the label text
- headerLabel->setText("<b>" + date.toString(Qt::DefaultLocaleLongDate) + "</b>");
- //set the button labels
- vorigeButton->setText(date.addDays(-1).toString());
- volgendeButton->setText(date.addDays(1).toString());
- //add entries to frame
- if(entries->count() > 0) {
- EntryWidget *widget;
- Plasma::Separator *sep;
- QTime currTime = QTime::currentTime();
- for(int i = 0 ; i < entries->size() ; i++) {
- //markeer de items die nu bezig zijn in het bold
- if(entries->at(i).starttijd.time() <= currTime && currTime <= entries->at(i).eindtijd.time()) {
- widget = new EntryWidget(entries->at(i), true);
- } else {
- widget = new EntryWidget(entries->at(i), false);
- }
- m_scrollerLayout->addItem(widget);
- //scheiding toevoegen
- if(i != entries->size()-1) {
- sep = new Plasma::Separator();
- sep->setOrientation(Qt::Horizontal);
- m_scrollerLayout->addItem(sep);
- }
- }
- } else {
- Plasma::Label *nothingLabel;
- nothingLabel = new Plasma::Label();
- nothingLabel->setText("Geen les vandaag! JOEPIE!");
- nothingLabel->setAlignment(Qt::AlignHCenter);
- m_scrollerLayout->addItem(nothingLabel);
- }
- }
- void KHLUurrooster::displayError(const QString& error)
- {
- //delete body frame entries
- QGraphicsLayoutItem *item;
- while(m_scrollerLayout->count() > 0) {
- item = m_scrollerLayout->itemAt(m_scrollerLayout->count() - 1);
- m_scrollerLayout->removeItem(item);
- delete item;
- }
- //display the error on applet
- Plasma::Label *errorLabel = new Plasma::Label();
- m_scrollerLayout->addItem(errorLabel);
- errorLabel->setText("<b>Fout:</b><br />" + error);
- }
- void KHLUurrooster::updateRoosters()
- {
- ical_busy = 1;
- rooster->removeEntries();
- rooster->update(ical_1, ical_1_color);
- }
- void KHLUurrooster::roosterError(const QString& error)
- {
- displayError(error);
- }
- void KHLUurrooster::sendNextIcalLink()
- {
- ical_busy += 1;
- if(ical_busy == 2) {
- rooster->update(ical_2, ical_2_color);
- } else if(ical_busy == 3) {
- rooster->update(ical_3, ical_3_color);
- } else {
- displayEntries();
- }
- }
- void KHLUurrooster::nextDate()
- {
- date = date.addDays(1);
- displayEntries();
- }
- void KHLUurrooster::prevDate()
- {
- date = date.addDays(-1);
- displayEntries();
- }
- #include "khluurrooster.moc"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement