~ubuntu-branches/ubuntu/natty/knemo/natty

« back to all changes in this revision

Viewing changes to src/kcm/statsconfig.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Felix Geyer
  • Date: 2011-02-22 16:36:22 UTC
  • mfrom: (1.1.13 upstream)
  • Revision ID: james.westby@ubuntu.com-20110222163622-d8i62gy1stn7tydv
Tags: 0.7.0-0ubuntu1
* New upstream release.
* Switch to source format 3.0 (quilt).
* Make knemo depend on libqt4-sql-sqlite.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of KNemo
 
2
   Copyright (C) 2010 John Stamp <jstamp@users.sourceforge.net>
 
3
 
 
4
   KNemo is free software; you can redistribute it and/or modify
 
5
   it under the terms of the GNU Library General Public License as
 
6
   published by the Free Software Foundation; either version 2 of
 
7
   the License, or (at your option) any later version.
 
8
 
 
9
   KNemo is distributed in the hope that it will be useful,
 
10
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
   GNU Library General Public License for more details.
 
13
 
 
14
   You should have received a copy of the GNU Library General Public License
 
15
   along with this library; see the file COPYING.LIB.  If not, write to
 
16
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
17
   Boston, MA 02110-1301, USA.
 
18
*/
 
19
 
 
20
#include "statsconfig.h"
 
21
#include <KCalendarSystem>
 
22
#include <KMessageBox>
 
23
 
 
24
StatsConfig::StatsConfig( const InterfaceSettings * settings, const KCalendarSystem *calendar, const StatsRule &rule, bool addRule ) : KDialog(),
 
25
    mSettings( settings ),
 
26
    mCal( calendar ),
 
27
    mAddRule( addRule )
 
28
{
 
29
    // Do this for the sake of KDateEdit
 
30
    KGlobal::locale()->setCalendar( mCal->calendarType() );
 
31
 
 
32
    mDlg.setupUi( mainWidget() );
 
33
    setButtons( KDialog::Default | KDialog::Ok | KDialog::Cancel );
 
34
 
 
35
    for ( int i = 1; i <= mCal->daysInWeek( QDate::currentDate() ); ++i )
 
36
    {
 
37
        mDlg.weekendStartDay->addItem( mCal->weekDayName( i ) );
 
38
        mDlg.weekendStopDay->addItem( mCal->weekDayName( i ) );
 
39
    }
 
40
 
 
41
    mDlg.periodUnits->addItem( i18n( "Days" ), KNemoStats::Day );
 
42
    mDlg.periodUnits->addItem( i18n( "Weeks" ), KNemoStats::Week );
 
43
    mDlg.periodUnits->addItem( i18n( "Months" ), KNemoStats::Month );
 
44
    //mDlg.periodUnits->addItem( i18n( "Years" ), KNemoStats::Year );
 
45
 
 
46
    connect( this, SIGNAL( defaultClicked() ), SLOT( setDefaults() ) );
 
47
    connect( mDlg.logOffpeak, SIGNAL( toggled( bool ) ), SLOT( enableItems() ) );
 
48
    connect( mDlg.doWeekend, SIGNAL( toggled( bool ) ), SLOT( enableItems() ) );
 
49
 
 
50
    QDate dt = rule.startDate;
 
51
    if ( !dt.isValid() )
 
52
        dt = QDate::currentDate().addDays( 1 - mCal->day( QDate::currentDate() ) );
 
53
    mDlg.startDate->setDate( dt );
 
54
    setControls( rule );
 
55
}
 
56
 
 
57
void StatsConfig::setControls( const StatsRule &s )
 
58
{
 
59
    mDlg.periodCount->setValue( s.periodCount );
 
60
    int index = mDlg.periodUnits->findData( s.periodUnits );
 
61
    mDlg.periodUnits->setCurrentIndex( index );
 
62
    mDlg.logOffpeak->setChecked( s.logOffpeak );
 
63
    mDlg.startTime->setTime( s.offpeakStartTime );
 
64
    mDlg.stopTime->setTime( s.offpeakEndTime );
 
65
    mDlg.doWeekend->setChecked( s.weekendIsOffpeak );
 
66
    mDlg.weekendStartDay->setCurrentIndex( s.weekendDayStart - 1 );
 
67
    mDlg.weekendStopDay->setCurrentIndex( s.weekendDayEnd - 1 );
 
68
    mDlg.weekendStartTime->setTime( s.weekendTimeStart );
 
69
    mDlg.weekendStopTime->setTime( s.weekendTimeEnd );
 
70
}
 
71
 
 
72
void StatsConfig::setDefaults()
 
73
{
 
74
    StatsRule s;
 
75
    mDlg.startDate->setDate( QDate::currentDate().addDays( 1 - mCal->day( QDate::currentDate() ) ) );
 
76
    setControls( s );
 
77
}
 
78
 
 
79
StatsRule StatsConfig::settings()
 
80
{
 
81
    StatsRule rule;
 
82
    rule.startDate = mDlg.startDate->date();
 
83
    rule.periodUnits = mDlg.periodUnits->itemData( mDlg.periodUnits->currentIndex() ).toInt();
 
84
    rule.periodCount = mDlg.periodCount->value();
 
85
    rule.logOffpeak = mDlg.logOffpeak->isChecked();
 
86
    rule.offpeakStartTime = mDlg.startTime->time();
 
87
    rule.offpeakEndTime = mDlg.stopTime->time();
 
88
    rule.weekendIsOffpeak = mDlg.doWeekend->isChecked();
 
89
    rule.weekendDayStart = mDlg.weekendStartDay->currentIndex() + 1;
 
90
    rule.weekendDayEnd = mDlg.weekendStopDay->currentIndex() + 1;
 
91
    rule.weekendTimeStart = mDlg.weekendStartTime->time();
 
92
    rule.weekendTimeEnd = mDlg.weekendStopTime->time();
 
93
    return rule;
 
94
}
 
95
 
 
96
void StatsConfig::enableItems()
 
97
{
 
98
    bool enabledItems;
 
99
    if ( mDlg.logOffpeak->isChecked() && mDlg.doWeekend->isChecked() )
 
100
    {
 
101
        enabledItems = true;
 
102
    }
 
103
    else
 
104
    {
 
105
        enabledItems = false;
 
106
    }
 
107
 
 
108
    mDlg.label_9->setEnabled( enabledItems );
 
109
    mDlg.label_10->setEnabled( enabledItems );
 
110
    mDlg.weekendStartDay->setEnabled( enabledItems );
 
111
    mDlg.weekendStopDay->setEnabled( enabledItems );
 
112
    mDlg.weekendStartTime->setEnabled( enabledItems );
 
113
    mDlg.weekendStopTime->setEnabled( enabledItems );
 
114
}
 
115
 
 
116
void StatsConfig::slotButtonClicked( int button )
 
117
{
 
118
    if ( mAddRule && ( (button == Ok) || (button == Apply) ) )
 
119
    {
 
120
        bool duplicateEntry = false;
 
121
        StatsRule testRule = settings();
 
122
        QList<StatsRule> statsRules = mSettings->statsRules;
 
123
        foreach ( StatsRule rule, statsRules )
 
124
        {
 
125
            if ( rule == testRule )
 
126
            {
 
127
                duplicateEntry = true;
 
128
                break;
 
129
            }
 
130
        }
 
131
        if ( duplicateEntry )
 
132
            KMessageBox::sorry( 0,
 
133
                                i18n( "Another rule already starts on %1. "
 
134
                                      "Please choose another date.",
 
135
                                      mCal->formatDate( mDlg.startDate->date(), KLocale::LongDate ) )
 
136
                              );
 
137
        else
 
138
            KDialog::slotButtonClicked( button );
 
139
    }
 
140
    else
 
141
        KDialog::slotButtonClicked( button );
 
142
}
 
143
 
 
144
#include "statsconfig.moc"