Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.95 KB | None | 0 0
  1. /*
  2.     Copyright (c) 2010, Stijn Leenknegt
  3.     All rights reserved.
  4.  
  5.     Redistribution and use in source and binary forms, with or without
  6.     modification, are permitted provided that the following conditions are met:
  7.         * Redistributions of source code must retain the above copyright
  8.         notice, this list of conditions and the following disclaimer.
  9.         * Redistributions in binary form must reproduce the above copyright
  10.         notice, this list of conditions and the following disclaimer in the
  11.         documentation and/or other materials provided with the distribution.
  12.         * Neither the name of the KHL nor the
  13.         names of its contributors may be used to endorse or promote products
  14.         derived from this software without specific prior written permission.
  15.  
  16.     THIS SOFTWARE IS PROVIDED BY Stijn Leenknegt ''AS IS'' AND ANY
  17.     EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18.     WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19.     DISCLAIMED. IN NO EVENT SHALL Stijn Leenknegt BE LIABLE FOR ANY
  20.     DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21.     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22.     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23.     ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24.     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25.     SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26.  
  27. */
  28.  
  29. #include <QPainter>
  30. #include <QFontMetrics>
  31. #include <QSizeF>
  32. #include <KLocale>
  33. #include <QWidget>
  34. #include <QString>
  35. #include <QList>
  36. #include <kconfigdialog.h>
  37. #include <kconfiggroup.h>
  38. #include <plasma/theme.h>
  39. #include <Plasma/Separator>
  40.  
  41. #include "entrywidget.h"
  42. #include "khluurrooster.h"
  43.  
  44.  
  45. KHLUurrooster::KHLUurrooster(QObject *parent, const QVariantList &args)
  46.     : Plasma::Applet(parent, args)
  47. {
  48.     // this will get us the standard applet background, for free!
  49.     setHasConfigurationInterface(true);
  50.     setBackgroundHints(DefaultBackground);
  51.     resize(300, 200);
  52.    
  53.     //setup the rooster object
  54.     rooster = new Uurrooster();
  55.     connect(rooster, SIGNAL(errorOccurred(QString)), this, SLOT(roosterError(QString)));
  56.     connect(rooster, SIGNAL(entriesChanged()), this, SLOT(sendNextIcalLink()));
  57.    
  58.     //set the timer
  59.     timer = new QTimer(this);
  60.     connect(timer, SIGNAL(timeout()), this, SLOT(updateRoosters()));
  61.    
  62.     //make layout for the widget
  63.     layout = new QGraphicsGridLayout(this);
  64.     setLayout(layout);
  65.    
  66.     //set date
  67.     date = QDate::currentDate();
  68.    
  69.     //set colors
  70.     ical_1_color = "black";
  71.     ical_2_color = "blue";
  72.     ical_3_color = "green";
  73.    
  74.     //set UI components
  75.     headerLabel = new Plasma::Label();
  76.     vorigeButton = new Plasma::PushButton();
  77.     volgendeButton = new Plasma::PushButton();
  78.     m_scrollWidget = new Plasma::ScrollWidget();//this?
  79.     m_scroller = new QGraphicsWidget();
  80.     m_scrollerLayout = new QGraphicsLinearLayout(Qt::Vertical, m_scroller);
  81.    
  82.     //configre the UI components
  83.     headerLabel->setAlignment(Qt::AlignCenter);
  84.     m_scrollWidget->setMinimumSize(300, 200);
  85.     m_scrollWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
  86.     m_scrollWidget->setWidget(m_scroller);
  87.    
  88.     //connect the buttons
  89.     connect(vorigeButton, SIGNAL(clicked()), this, SLOT(prevDate()));
  90.     connect(volgendeButton, SIGNAL(clicked()), this, SLOT(nextDate()));
  91.    
  92.     //add to layout
  93.     layout->addItem(headerLabel, 0, 0, 1, 2);
  94.     layout->addItem(m_scrollWidget, 1, 0, 1, 2);
  95.     layout->addItem(vorigeButton, 2, 0);
  96.     layout->addItem(volgendeButton, 2, 1);
  97. }
  98.  
  99.  
  100. KHLUurrooster::~KHLUurrooster()
  101. {
  102. }
  103.  
  104.  
  105. void KHLUurrooster::init()
  106. {
  107.     readConfig();
  108.    
  109.     if(ical_1.isEmpty()) { //no configuration setted yet
  110.       this->setConfigurationRequired(true, "Je moet op zijn minst 1 iCal link opgeven");
  111.     } else {
  112.       configChanged();
  113.     }
  114. }
  115.  
  116.  
  117. void KHLUurrooster::createConfigurationInterface(KConfigDialog *parent)
  118. {
  119.     QWidget *widget = new QWidget(0);
  120.     ui.setupUi(widget);
  121.    
  122.     //als er op ok of apply gedrukt is, moet de config opgeslaan worden
  123.     connect(parent, SIGNAL(okClicked()), this, SLOT(configAccepted()));
  124.     connect(parent, SIGNAL(applyClicked()), this, SLOT(configAccepted()));
  125.    
  126.     //stel de value van de textbox in
  127.     ui.txtIcal_1->setText(ical_1);
  128.     ui.txtIcal_2->setText(ical_2);
  129.     ui.txtIcal_3->setText(ical_3);
  130.    
  131.     parent->addPage(widget, "KHL Uurrooster", icon());
  132. }
  133.  
  134.  
  135. void KHLUurrooster::readConfig()
  136. {
  137.     KConfigGroup cg = config();
  138.     ical_1 = cg.readEntry("ical_1");
  139.     ical_2 = cg.readEntry("ical_2");
  140.     ical_3 = cg.readEntry("ical_3");
  141. }
  142.  
  143.  
  144. void KHLUurrooster::configChanged()
  145. {
  146.     //stop the timer if needed
  147.     if(timer->isActive()) {
  148.       timer->stop();
  149.     }
  150.    
  151.     //get the config
  152.     readConfig();
  153.    
  154.     //check if filled in
  155.     if(! ical_1.isEmpty()) { //no configuration setted yet
  156.       this->setConfigurationRequired(false, "Je moet op zijn minst 1 iCal link opgeven");
  157.     }
  158.    
  159.     //update het uurrooster, hier wordt het ical terug opgehaald.
  160.     ical_busy = 1;
  161.     rooster->removeEntries();
  162.     rooster->update(ical_1, ical_1_color);
  163.    
  164.     //start the timer with a given timeout, default it's once a hour
  165.     timer->start(60 * 60 * 1000);
  166. }
  167.  
  168.  
  169. void KHLUurrooster::configAccepted()
  170. {
  171.     ical_1 = ui.txtIcal_1->text();
  172.     ical_2 = ui.txtIcal_2->text();
  173.     ical_3 = ui.txtIcal_3->text();
  174.    
  175.     KConfigGroup cg = config();
  176.     cg.writeEntry("ical_1", ical_1);
  177.     cg.writeEntry("ical_2", ical_2);
  178.     cg.writeEntry("ical_3", ical_3);
  179.    
  180.     emit configNeedsSaving();
  181. }
  182.  
  183.  
  184. void KHLUurrooster::paintInterface(QPainter *p, const QStyleOptionGraphicsItem *option, const QRect &contentsRect)
  185. {
  186.     p->setRenderHint(QPainter::SmoothPixmapTransform);
  187.     p->setRenderHint(QPainter::Antialiasing);
  188.     layout->setGeometry(contentsRect);
  189. }
  190.  
  191.  
  192. void KHLUurrooster::displayEntries()
  193. {
  194.     //de uurrooster entries, mooi gesorteerd.
  195.     QList<uurrooster_entry> *entries= rooster->getEntries(date);
  196.    
  197.     //delete body frame entries
  198.     QGraphicsLayoutItem *item;
  199.     while(m_scrollerLayout->count() > 0) {
  200.       item = m_scrollerLayout->itemAt(m_scrollerLayout->count() - 1);
  201.       m_scrollerLayout->removeItem(item);
  202.       delete item;
  203.     }
  204.    
  205.     //set the label text
  206.     headerLabel->setText("<b>" + date.toString(Qt::DefaultLocaleLongDate) + "</b>");
  207.    
  208.     //set the button labels
  209.     vorigeButton->setText(date.addDays(-1).toString());
  210.     volgendeButton->setText(date.addDays(1).toString());
  211.    
  212.     //add entries to frame
  213.     if(entries->count() > 0) {
  214.       EntryWidget *widget;
  215.       Plasma::Separator *sep;
  216.       QTime currTime = QTime::currentTime();
  217.       for(int i = 0 ; i < entries->size() ; i++) {
  218.         //markeer de items die nu bezig zijn in het bold
  219.         if(entries->at(i).starttijd.time() <= currTime && currTime <= entries->at(i).eindtijd.time()) {
  220.           widget = new EntryWidget(entries->at(i), true);
  221.         } else {
  222.           widget = new EntryWidget(entries->at(i), false);
  223.         }
  224.         m_scrollerLayout->addItem(widget);
  225.        
  226.         //scheiding toevoegen
  227.         if(i != entries->size()-1) {
  228.           sep = new Plasma::Separator();
  229.           sep->setOrientation(Qt::Horizontal);
  230.           m_scrollerLayout->addItem(sep);
  231.         }
  232.       }
  233.     } else {
  234.       Plasma::Label *nothingLabel;
  235.       nothingLabel = new Plasma::Label();
  236.       nothingLabel->setText("Geen les vandaag! JOEPIE!");
  237.       nothingLabel->setAlignment(Qt::AlignHCenter);
  238.       m_scrollerLayout->addItem(nothingLabel);
  239.     }
  240. }
  241.  
  242.  
  243. void KHLUurrooster::displayError(const QString& error)
  244. {
  245.     //delete body frame entries
  246.     QGraphicsLayoutItem *item;
  247.     while(m_scrollerLayout->count() > 0) {
  248.       item = m_scrollerLayout->itemAt(m_scrollerLayout->count() - 1);
  249.       m_scrollerLayout->removeItem(item);
  250.       delete item;
  251.     }
  252.    
  253.     //display the error on applet
  254.     Plasma::Label *errorLabel = new Plasma::Label();
  255.     m_scrollerLayout->addItem(errorLabel);
  256.     errorLabel->setText("<b>Fout:</b><br />" + error);
  257. }
  258.  
  259.  
  260. void KHLUurrooster::updateRoosters()
  261. {
  262.     ical_busy = 1;
  263.     rooster->removeEntries();
  264.     rooster->update(ical_1, ical_1_color);
  265. }
  266.  
  267.  
  268. void KHLUurrooster::roosterError(const QString& error)
  269. {
  270.     displayError(error);
  271. }
  272.  
  273.  
  274. void KHLUurrooster::sendNextIcalLink()
  275. {
  276.     ical_busy += 1;
  277.  
  278.     if(ical_busy == 2) {
  279.       rooster->update(ical_2, ical_2_color);
  280.     } else if(ical_busy == 3) {
  281.       rooster->update(ical_3, ical_3_color);
  282.     } else {
  283.       displayEntries();
  284.     }
  285. }
  286.  
  287.  
  288. void KHLUurrooster::nextDate()
  289. {
  290.     date = date.addDays(1);
  291.     displayEntries();
  292. }
  293.  
  294.  
  295. void KHLUurrooster::prevDate()
  296. {
  297.     date = date.addDays(-1);
  298.     displayEntries();
  299. }
  300.  
  301.  
  302. #include "khluurrooster.moc"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement