2014-04-09 21:01:57 +00:00
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
* vim : set ts = 8 sts = 4 et sw = 4 tw = 99 :
*
* SleepLib Profiles Implementation
*
* Copyright ( c ) 2011 - 2014 Mark Watkins < jedimark @ users . sourceforge . net >
*
* This file is subject to the terms and conditions of the GNU General Public
* License . See the file COPYING in the main directory of the Linux
* distribution for more details . */
2011-06-26 08:30:44 +00:00
# include <QString>
# include <QDateTime>
# include <QDir>
# include <QMessageBox>
2011-07-01 10:10:44 +00:00
# include <QDebug>
2012-01-02 15:34:17 +00:00
# include <algorithm>
2012-01-05 04:37:22 +00:00
# include <cmath>
2011-06-26 08:30:44 +00:00
# include "preferences.h"
# include "profiles.h"
# include "machine.h"
# include "machine_loader.h"
2011-12-11 09:45:28 +00:00
# include <QApplication>
# include "mainwindow.h"
2014-04-17 05:58:57 +00:00
extern MainWindow * mainwin ;
2011-06-26 08:30:44 +00:00
Preferences * p_pref ;
Preferences * p_layout ;
2014-04-17 05:58:57 +00:00
Profile * p_profile ;
2011-06-26 08:30:44 +00:00
Profile : : Profile ( QString path )
2014-04-25 05:28:10 +00:00
: is_first_day ( true ) ,
m_opened ( false )
2014-04-17 05:58:57 +00:00
{
p_name = STR_GEN_Profile ;
if ( path . isEmpty ( ) ) {
p_path = GetAppRoot ( ) ;
2014-04-20 05:25:09 +00:00
} else {
p_path = path ;
}
2014-04-17 05:58:57 +00:00
( * this ) [ STR_GEN_DataFolder ] = p_path ;
path = path . replace ( " \\ " , " / " ) ;
if ( ! p_path . endsWith ( " / " ) ) {
p_path + = " / " ;
}
p_filename = p_path + p_name + STR_ext_XML ;
2011-06-26 08:30:44 +00:00
machlist . clear ( ) ;
2011-12-21 11:09:50 +00:00
2014-04-23 13:19:56 +00:00
doctor = nullptr ;
user = nullptr ;
cpap = nullptr ;
oxi = nullptr ;
appearance = nullptr ;
session = nullptr ;
general = nullptr ;
2011-06-26 08:30:44 +00:00
}
2011-12-11 09:45:28 +00:00
2011-12-11 10:59:30 +00:00
Profile : : ~ Profile ( )
{
2014-04-25 05:28:10 +00:00
if ( m_opened ) {
delete user ;
delete doctor ;
delete cpap ;
delete oxi ;
delete appearance ;
delete session ;
delete general ;
2014-05-06 18:03:13 +00:00
for ( QHash < MachineID , Machine * > : : iterator it = machlist . begin ( ) ; it ! = machlist . end ( ) ; it + + ) {
2014-04-25 05:28:10 +00:00
delete it . value ( ) ;
}
m_opened = false ;
2011-06-26 08:30:44 +00:00
}
}
2012-01-12 22:46:06 +00:00
2014-04-20 05:25:09 +00:00
bool Profile : : Save ( QString filename )
{
2014-04-25 05:28:10 +00:00
if ( m_opened ) {
return Preferences : : Save ( filename ) ;
} else return false ;
2014-04-20 05:25:09 +00:00
}
2012-01-12 22:46:06 +00:00
bool Profile : : Open ( QString filename )
{
2014-04-25 05:28:10 +00:00
if ( filename . isEmpty ( ) ) {
filename = p_filename ;
}
if ( m_opened ) {
qDebug ( ) < < " Profile " < < filename < < " all ready open " ;
return true ;
}
2014-04-17 05:58:57 +00:00
bool b = Preferences : : Open ( filename ) ;
2014-04-25 05:28:10 +00:00
m_opened = true ;
2014-04-17 05:58:57 +00:00
doctor = new DoctorInfo ( this ) ;
user = new UserInfo ( this ) ;
cpap = new CPAPSettings ( this ) ;
oxi = new OxiSettings ( this ) ;
appearance = new AppearanceSettings ( this ) ;
session = new SessionSettings ( this ) ;
general = new UserSettings ( this ) ;
2012-01-12 22:46:06 +00:00
return b ;
}
2011-07-29 22:55:24 +00:00
void Profile : : DataFormatError ( Machine * m )
{
2014-04-17 05:58:57 +00:00
QString msg =
QObject : : tr ( " Software changes have been made that require the reimporting of the following machines data: \n \n " ) ;
msg = msg + m - > properties [ STR_PROP_Brand ] + " " + m - > properties [ STR_PROP_Model ] + " " +
m - > properties [ STR_PROP_Serial ] ;
msg = msg +
QObject : : tr ( " I can automatically purge this data for you, or you can cancel now and continue to run in a previous version. \n \n " ) ;
msg = msg +
QObject : : tr ( " Would you like me to purge this data this for you so you can run the new version? " ) ;
2014-04-23 13:19:56 +00:00
if ( QMessageBox : : warning ( nullptr , QObject : : tr ( " Machine Database Changes " ) , msg ,
2014-04-17 05:58:57 +00:00
QMessageBox : : Yes | QMessageBox : : Cancel , QMessageBox : : Yes ) = = QMessageBox : : Yes ) {
if ( ! m - > Purge (
3478216 ) ) { // Do not copy this line without thinking.. You will be eaten by a Grue if you do
2014-04-23 13:19:56 +00:00
QMessageBox : : critical ( nullptr , QObject : : tr ( " Purge Failed " ) ,
2014-04-17 05:58:57 +00:00
QObject : : tr ( " Sorry, I could not purge this data, which means this version of SleepyHead can't start.. SleepyHead's Data folder needs to be removed manually \n \n This folder currently resides at the following location: \n " )
+ PREF [ STR_GEN_DataFolder ] . toString ( ) , QMessageBox : : Ok ) ;
2012-01-06 16:07:54 +00:00
QApplication : : exit ( - 1 ) ;
2011-07-29 22:55:24 +00:00
}
} else {
2012-01-06 16:07:54 +00:00
QApplication : : exit ( - 1 ) ;
2011-07-29 22:55:24 +00:00
}
2014-04-17 05:58:57 +00:00
2011-07-29 22:55:24 +00:00
return ;
}
2011-06-26 08:30:44 +00:00
void Profile : : LoadMachineData ( )
{
2014-04-17 05:58:57 +00:00
QHash < MachineID , QMap < QDate , QHash < ChannelID , EventDataType > > > cache ;
2011-12-11 09:45:28 +00:00
2014-04-17 05:58:57 +00:00
for ( QHash < MachineID , Machine * > : : iterator i = machlist . begin ( ) ; i ! = machlist . end ( ) ; i + + ) {
Machine * m = i . value ( ) ;
MachineLoader * loader = GetLoader ( m - > GetClass ( ) ) ;
2011-06-26 08:30:44 +00:00
if ( loader ) {
2014-04-17 05:58:57 +00:00
long v = loader - > Version ( ) ;
long cv = 0 ;
if ( m - > properties . find ( STR_PROP_DataVersion ) = = m - > properties . end ( ) ) {
m - > properties [ STR_PROP_DataVersion ] = " 0 " ;
2011-06-26 08:30:44 +00:00
}
2014-04-17 05:58:57 +00:00
2011-06-26 08:30:44 +00:00
bool ok ;
2014-04-17 05:58:57 +00:00
cv = m - > properties [ STR_PROP_DataVersion ] . toLong ( & ok ) ;
if ( ! ok | | cv < v ) {
2011-07-29 22:55:24 +00:00
DataFormatError ( m ) ;
// It may exit above and not return here..
QString s ;
2014-04-17 05:58:57 +00:00
s . sprintf ( " %li " , v ) ;
m - > properties [ STR_PROP_DataVersion ] = s ; // Dont need to nag again if they are too lazy.
2011-07-29 22:55:24 +00:00
} else {
try {
m - > Load ( ) ;
2014-04-17 05:58:57 +00:00
} catch ( OldDBVersion e ) {
2014-05-08 04:46:23 +00:00
Q_UNUSED ( e )
2011-07-29 22:55:24 +00:00
DataFormatError ( m ) ;
2011-06-26 08:30:44 +00:00
}
2011-07-29 22:55:24 +00:00
}
2011-06-26 08:30:44 +00:00
} else {
m - > Load ( ) ;
}
}
}
/**
* @ brief Machine XML section in profile .
* @ param root
*/
2014-04-17 05:58:57 +00:00
void Profile : : ExtraLoad ( QDomElement & root )
2011-06-26 08:30:44 +00:00
{
2014-04-17 05:58:57 +00:00
if ( root . tagName ( ) ! = " Machines " ) {
2011-07-24 16:34:53 +00:00
qDebug ( ) < < " No Machines Tag in Profiles.xml " ;
return ;
2011-06-26 08:30:44 +00:00
}
2014-04-17 05:58:57 +00:00
QDomElement elem = root . firstChildElement ( ) ;
2011-07-24 16:34:53 +00:00
while ( ! elem . isNull ( ) ) {
2014-04-17 05:58:57 +00:00
QString pKey = elem . tagName ( ) ;
2011-07-24 16:34:53 +00:00
2014-04-17 05:58:57 +00:00
if ( pKey ! = " Machine " ) {
2011-07-19 15:31:51 +00:00
qWarning ( ) < < " Profile::ExtraLoad() pKey!= \" Machine \" " ;
2014-04-17 05:58:57 +00:00
elem = elem . nextSiblingElement ( ) ;
2011-07-19 15:31:51 +00:00
continue ;
}
2014-04-17 05:58:57 +00:00
2011-06-26 08:30:44 +00:00
int m_id ;
2011-07-24 16:34:53 +00:00
bool ok ;
2014-04-17 05:58:57 +00:00
m_id = elem . attribute ( " id " , " " ) . toInt ( & ok ) ;
2011-06-26 08:30:44 +00:00
int mt ;
2014-04-17 05:58:57 +00:00
mt = elem . attribute ( " type " , " " ) . toInt ( & ok ) ;
MachineType m_type = ( MachineType ) mt ;
2011-07-24 16:34:53 +00:00
2014-04-17 05:58:57 +00:00
QString m_class = elem . attribute ( " class " , " " ) ;
2011-07-24 16:34:53 +00:00
//MachineLoader *ml=GetLoader(m_class);
2011-06-26 08:30:44 +00:00
Machine * m ;
2014-04-17 05:58:57 +00:00
2011-07-24 16:34:53 +00:00
//if (ml) {
// ml->CreateMachine
//}
2014-04-17 05:58:57 +00:00
if ( m_type = = MT_CPAP ) {
m = new CPAP ( this , m_id ) ;
} else if ( m_type = = MT_OXIMETER ) {
m = new Oximeter ( this , m_id ) ;
} else if ( m_type = = MT_SLEEPSTAGE ) {
m = new SleepStage ( this , m_id ) ;
} else if ( m_type = = MT_POSITION ) {
m = new PositionSensor ( this , m_id ) ;
} else {
m = new Machine ( this , m_id ) ;
2011-06-26 08:30:44 +00:00
m - > SetType ( m_type ) ;
}
2014-04-17 05:58:57 +00:00
2011-06-26 08:30:44 +00:00
m - > SetClass ( m_class ) ;
AddMachine ( m ) ;
2014-04-17 05:58:57 +00:00
QDomElement e = elem . firstChildElement ( ) ;
for ( ; ! e . isNull ( ) ; e = e . nextSiblingElement ( ) ) {
QString pKey = e . tagName ( ) ;
m - > properties [ pKey ] = e . text ( ) ;
2011-06-26 08:30:44 +00:00
}
2014-04-17 05:58:57 +00:00
elem = elem . nextSiblingElement ( ) ;
2011-06-26 08:30:44 +00:00
}
}
2014-04-17 05:58:57 +00:00
void Profile : : AddMachine ( Machine * m )
{
2011-07-24 16:34:53 +00:00
if ( ! m ) {
qWarning ( ) < < " Empty Machine in Profile::AddMachine() " ;
return ;
}
2014-04-17 05:58:57 +00:00
machlist [ m - > id ( ) ] = m ;
2011-06-26 08:30:44 +00:00
} ;
2014-04-17 05:58:57 +00:00
void Profile : : DelMachine ( Machine * m )
{
2011-07-24 16:34:53 +00:00
if ( ! m ) {
qWarning ( ) < < " Empty Machine in Profile::AddMachine() " ;
return ;
}
2014-04-17 05:58:57 +00:00
2011-07-31 20:24:43 +00:00
machlist . erase ( machlist . find ( m - > id ( ) ) ) ;
2011-06-26 08:30:44 +00:00
} ;
2011-07-24 16:34:53 +00:00
// Potential Memory Leak Here..
2014-04-17 05:58:57 +00:00
QDomElement Profile : : ExtraSave ( QDomDocument & doc )
{
QDomElement mach = doc . createElement ( " Machines " ) ;
for ( QHash < MachineID , Machine * > : : iterator i = machlist . begin ( ) ; i ! = machlist . end ( ) ; i + + ) {
QDomElement me = doc . createElement ( " Machine " ) ;
Machine * m = i . value ( ) ;
me . setAttribute ( " id " , ( int ) m - > id ( ) ) ;
me . setAttribute ( " type " , ( int ) m - > GetType ( ) ) ;
me . setAttribute ( " class " , m - > GetClass ( ) ) ;
if ( ! m - > properties . contains ( STR_PROP_Path ) ) { m - > properties [ STR_PROP_Path ] = " {DataFolder}/ " + m - > GetClass ( ) + " _ " + m - > hexid ( ) ; }
for ( QHash < QString , QString > : : iterator j = i . value ( ) - > properties . begin ( ) ;
j ! = i . value ( ) - > properties . end ( ) ; j + + ) {
QDomElement mp = doc . createElement ( j . key ( ) ) ;
2011-07-31 20:24:43 +00:00
mp . appendChild ( doc . createTextNode ( j . value ( ) ) ) ;
2011-07-24 16:34:53 +00:00
//mp->LinkEndChild(new QDomText(j->second.toLatin1()));
me . appendChild ( mp ) ;
2011-06-26 08:30:44 +00:00
}
2014-04-17 05:58:57 +00:00
2011-07-24 16:34:53 +00:00
mach . appendChild ( me ) ;
2011-06-26 08:30:44 +00:00
}
2014-04-17 05:58:57 +00:00
2011-06-26 08:30:44 +00:00
return mach ;
}
2014-04-17 05:58:57 +00:00
void Profile : : AddDay ( QDate date , Day * day , MachineType mt )
{
2011-06-26 08:30:44 +00:00
//date+=wxTimeSpan::Day();
2012-01-08 03:29:22 +00:00
if ( ! day ) {
qDebug ( ) < < " Profile::AddDay called with null day object " ;
return ;
}
2014-04-17 05:58:57 +00:00
2011-06-26 08:30:44 +00:00
if ( is_first_day ) {
2014-04-17 05:58:57 +00:00
m_first = m_last = date ;
is_first_day = false ;
}
if ( m_first > date ) {
m_first = date ;
}
if ( m_last < date ) {
m_last = date ;
2011-06-26 08:30:44 +00:00
}
// Check for any other machines of same type.. Throw an exception if one already exists.
2014-04-17 05:58:57 +00:00
QList < Day * > & dl = daylist [ date ] ;
for ( QList < Day * > : : iterator a = dl . begin ( ) ; a ! = dl . end ( ) ; a + + ) {
if ( ( * a ) - > machine - > GetType ( ) = = mt ) {
2012-04-05 03:14:02 +00:00
// disabled this because two machines isn't all that bad
2014-04-23 13:19:56 +00:00
// if (QMessageBox::question(nullptr,"Different Machine Detected","This data comes from another machine to what's usually imported, and has overlapping data.\nThis new data will override any older data from the old machine. Are you sure you want to do this?",QMessageBox::Yes,QMessageBox::No)==QMessageBox::No) {
2012-04-05 03:14:02 +00:00
// throw OneTypePerDay();
// }
2011-12-24 05:55:44 +00:00
daylist [ date ] . erase ( a ) ;
break ;
2011-06-26 08:30:44 +00:00
}
}
2014-04-17 05:58:57 +00:00
2011-06-26 08:30:44 +00:00
daylist [ date ] . push_back ( day ) ;
}
2014-04-11 22:22:23 +00:00
// Get Day record if data available for date and machine type,
2014-04-23 13:19:56 +00:00
// and has enabled session data, else return nullptr
2014-04-17 05:58:57 +00:00
Day * Profile : : GetGoodDay ( QDate date , MachineType type )
2011-12-28 12:36:40 +00:00
{
2014-04-11 22:22:23 +00:00
Day * day = GetDay ( date , type ) ;
if ( ! day )
2014-04-23 13:19:56 +00:00
return nullptr ;
2014-04-11 22:22:23 +00:00
// Just return the day if not matching for a machine.
if ( type = = MT_UNKNOWN )
return day ;
// For a machine match, find at least one enabled Session.
Q_ASSERT ( day - > machine_type ( ) = = type ) ;
for ( int i = 0 ; i < day - > size ( ) ; + + i ) {
if ( ( * day ) [ i ] - > enabled ( ) )
return day ;
2011-12-28 12:36:40 +00:00
}
2014-04-17 05:58:57 +00:00
2014-04-11 22:22:23 +00:00
// No enabled Sessions were found.
2014-04-23 13:19:56 +00:00
return nullptr ;
2011-12-28 12:36:40 +00:00
}
2014-04-17 05:58:57 +00:00
Day * Profile : : GetDay ( QDate date , MachineType type )
2011-06-26 08:30:44 +00:00
{
2014-04-11 22:22:23 +00:00
if ( ! daylist . contains ( date ) )
2014-04-23 13:19:56 +00:00
return nullptr ;
2014-04-17 05:58:57 +00:00
2014-04-11 22:22:23 +00:00
QList < Day * > list ( daylist . value ( date ) ) ;
2014-04-22 07:16:24 +00:00
QList < Day * > : : iterator it = list . begin ( ) ;
QList < Day * > : : iterator list_end = list . end ( ) ;
for ( ; it ! = list_end ; + + it ) {
Day * day = ( * it ) ;
2014-04-23 13:19:56 +00:00
Q_ASSERT ( day ! = nullptr ) ;
2014-04-17 05:58:57 +00:00
2014-04-11 22:22:23 +00:00
// Just return the day if not matching for a machine.
if ( day - > machine_type ( ) = = type | | type = = MT_UNKNOWN ) {
return day ;
2011-06-26 08:30:44 +00:00
}
}
2014-04-17 05:58:57 +00:00
2014-04-23 13:19:56 +00:00
return nullptr ;
2011-06-26 08:30:44 +00:00
}
2011-07-15 13:30:41 +00:00
int Profile : : Import ( QString path )
2011-06-26 08:30:44 +00:00
{
2014-04-17 05:58:57 +00:00
int c = 0 ;
2011-07-01 10:10:44 +00:00
qDebug ( ) < < " Importing " < < path ;
2014-04-17 05:58:57 +00:00
path = path . replace ( " \\ " , " / " ) ;
if ( path . endsWith ( " / " ) ) {
2013-09-11 12:57:06 +00:00
path . chop ( 1 ) ;
2014-04-17 05:58:57 +00:00
}
QList < MachineLoader * > loaders = GetLoaders ( ) ;
2011-11-30 06:01:38 +00:00
2014-04-22 07:16:24 +00:00
Q_FOREACH ( MachineLoader * loader , loaders ) {
if ( c + = loader - > Open ( path , this ) ) {
2013-09-11 12:57:06 +00:00
break ;
2014-04-17 05:58:57 +00:00
}
2011-06-26 08:30:44 +00:00
}
2014-04-17 05:58:57 +00:00
2011-07-15 13:30:41 +00:00
return c ;
2011-06-26 08:30:44 +00:00
}
2014-04-17 05:58:57 +00:00
MachineLoader * GetLoader ( QString name )
2011-06-26 08:30:44 +00:00
{
2014-04-17 05:58:57 +00:00
QList < MachineLoader * > loaders = GetLoaders ( ) ;
2014-04-22 07:16:24 +00:00
Q_FOREACH ( MachineLoader * loader , loaders ) {
if ( loader - > ClassName ( ) = = name ) {
return loader ;
2011-06-26 08:30:44 +00:00
}
}
2014-04-17 05:58:57 +00:00
2014-04-23 13:19:56 +00:00
return nullptr ;
2011-06-26 08:30:44 +00:00
}
2011-07-31 20:24:43 +00:00
// Returns a QVector containing all machine objects regisered of type t
2013-09-14 23:32:14 +00:00
QList < Machine * > Profile : : GetMachines ( MachineType t )
2011-06-26 08:30:44 +00:00
{
2011-12-08 04:10:35 +00:00
QList < Machine * > vec ;
2014-04-17 05:58:57 +00:00
QHash < MachineID , Machine * > : : iterator i ;
2014-04-22 07:16:24 +00:00
QHash < MachineID , Machine * > : : iterator machlist_end = machlist . end ( ) ;
2011-06-26 08:30:44 +00:00
2014-04-22 07:16:24 +00:00
for ( i = machlist . begin ( ) ; i ! = machlist_end ; i + + ) {
2011-07-31 20:24:43 +00:00
if ( ! i . value ( ) ) {
2014-04-23 13:19:56 +00:00
qWarning ( ) < < " Profile::GetMachines() i->second == nullptr " ;
2011-07-19 15:31:51 +00:00
continue ;
}
2014-04-17 05:58:57 +00:00
MachineType mt = i . value ( ) - > GetType ( ) ;
if ( ( t = = MT_UNKNOWN ) | | ( mt = = t ) ) {
2011-07-31 20:24:43 +00:00
vec . push_back ( i . value ( ) ) ;
2011-06-26 08:30:44 +00:00
}
}
2014-04-17 05:58:57 +00:00
2011-06-26 08:30:44 +00:00
return vec ;
}
2014-04-17 05:58:57 +00:00
Machine * Profile : : GetMachine ( MachineType t )
2011-06-26 08:30:44 +00:00
{
2014-04-17 05:58:57 +00:00
QList < Machine * > vec = GetMachines ( t ) ;
if ( vec . size ( ) = = 0 ) {
2014-04-23 13:19:56 +00:00
return nullptr ;
2014-04-17 05:58:57 +00:00
}
2011-06-26 08:30:44 +00:00
return vec [ 0 ] ;
}
2014-04-17 05:58:57 +00:00
void Profile : : RemoveSession ( Session * sess )
2011-12-06 14:39:14 +00:00
{
2014-04-17 05:58:57 +00:00
QMap < QDate , QList < Day * > > : : iterator di ;
2014-04-22 07:16:24 +00:00
QMap < QDate , QList < Day * > > : : iterator daylist_end = daylist . end ( ) ;
2011-12-06 14:39:14 +00:00
2014-04-22 07:16:24 +00:00
for ( di = daylist . begin ( ) ; di ! = daylist_end ; di + + ) {
2014-04-17 05:58:57 +00:00
for ( int d = 0 ; d < di . value ( ) . size ( ) ; d + + ) {
Day * day = di . value ( ) [ d ] ;
2011-12-06 14:39:14 +00:00
2014-04-17 05:58:57 +00:00
int i = day - > getSessions ( ) . indexOf ( sess ) ;
if ( i > = 0 ) {
for ( ; i < day - > getSessions ( ) . size ( ) - 1 ; i + + ) {
day - > getSessions ( ) [ i ] = day - > getSessions ( ) [ i + 1 ] ;
2011-12-06 14:39:14 +00:00
}
2014-04-17 05:58:57 +00:00
2011-12-06 14:39:14 +00:00
day - > getSessions ( ) . pop_back ( ) ;
2014-04-17 05:58:57 +00:00
qint64 first = 0 , last = 0 ;
for ( int i = 0 ; i < day - > getSessions ( ) . size ( ) ; i + + ) {
Session & sess = * day - > getSessions ( ) [ i ] ;
if ( ! first | | first > sess . first ( ) ) {
first = sess . first ( ) ;
}
if ( ! last | | last < sess . last ( ) ) {
last = sess . last ( ) ;
}
2011-12-06 14:39:14 +00:00
}
2014-04-17 05:58:57 +00:00
2012-01-19 15:18:34 +00:00
// day->setFirst(first);
// day->setLast(last);
2011-12-06 14:39:14 +00:00
return ;
}
}
}
}
2014-04-23 13:19:56 +00:00
//Profile *profile=nullptr;
2011-06-26 08:30:44 +00:00
QString SHA1 ( QString pass )
{
return pass ;
}
2014-04-17 05:58:57 +00:00
namespace Profiles {
2011-06-26 08:30:44 +00:00
2014-04-17 05:58:57 +00:00
QMap < QString , Profile * > profiles ;
2011-06-26 08:30:44 +00:00
void Done ( )
{
2011-10-05 07:41:56 +00:00
PREF . Save ( ) ;
LAYOUT . Save ( ) ;
2014-04-17 05:58:57 +00:00
2014-04-22 07:16:24 +00:00
Q_FOREACH ( Profile * profile , profiles ) {
// TODO: only save open profiles.. (maybe add an open bit?)
profile - > Save ( ) ;
delete profile ;
2011-06-26 08:30:44 +00:00
}
2014-04-17 05:58:57 +00:00
2011-06-26 08:30:44 +00:00
profiles . clear ( ) ;
delete p_pref ;
delete p_layout ;
2013-01-18 20:18:25 +00:00
DestroyLoaders ( ) ;
2011-06-26 08:30:44 +00:00
}
Profile * Get ( QString name )
{
2014-04-17 05:58:57 +00:00
if ( profiles . find ( name ) ! = profiles . end ( ) ) {
2011-06-26 08:30:44 +00:00
return profiles [ name ] ;
2014-04-17 05:58:57 +00:00
}
2011-06-26 08:30:44 +00:00
2014-04-23 13:19:56 +00:00
return nullptr ;
2011-06-26 08:30:44 +00:00
}
2011-10-01 12:54:20 +00:00
Profile * Create ( QString name )
2011-06-26 08:30:44 +00:00
{
2014-04-17 05:58:57 +00:00
QString path = PREF . Get ( " {home}/Profiles/ " ) + name ;
2011-06-26 08:30:44 +00:00
QDir dir ( path ) ;
2014-04-17 05:58:57 +00:00
if ( ! dir . exists ( path ) ) {
2013-09-11 12:57:06 +00:00
dir . mkpath ( path ) ;
2014-04-17 05:58:57 +00:00
}
2011-06-26 08:30:44 +00:00
//path+="/"+name;
2014-04-17 05:58:57 +00:00
Profile * prof = new Profile ( path ) ;
2011-06-26 08:30:44 +00:00
prof - > Open ( ) ;
2014-04-17 05:58:57 +00:00
profiles [ name ] = prof ;
2011-12-21 14:24:09 +00:00
prof - > user - > setUserName ( name ) ;
2011-09-08 09:50:05 +00:00
//prof->Set("Realname",realname);
2011-12-21 14:24:09 +00:00
//if (!password.isEmpty()) prof.user->setPassword(password);
2014-04-17 05:58:57 +00:00
prof - > Set ( STR_GEN_DataFolder , QString ( " {home}/Profiles/{ " ) + QString ( STR_UI_UserName ) +
QString ( " } " ) ) ;
2011-06-26 08:30:44 +00:00
2014-04-17 05:58:57 +00:00
Machine * m = new Machine ( prof , 0 ) ;
2011-06-26 08:30:44 +00:00
m - > SetClass ( " Journal " ) ;
2014-04-17 05:58:57 +00:00
m - > properties [ STR_PROP_Brand ] = " Journal " ;
m - > properties [ STR_PROP_Model ] = " Journal Data Machine Object " ;
m - > properties [ STR_PROP_Serial ] = m - > hexid ( ) ;
m - > properties [ STR_PROP_Path ] = " {DataFolder}/ " + m - > GetClass ( ) + " _ " + m - > hexid ( ) ;
2011-06-26 08:30:44 +00:00
m - > SetType ( MT_JOURNAL ) ;
prof - > AddMachine ( m ) ;
prof - > Save ( ) ;
return prof ;
}
Profile * Get ( )
{
2011-06-27 06:26:29 +00:00
// username lookup
//getUserName()
return profiles [ getUserName ( ) ] ; ;
2011-06-26 08:30:44 +00:00
}
/**
* @ brief Scan Profile directory loading user profiles
*/
void Scan ( )
{
2014-04-17 05:58:57 +00:00
QString path = PREF . Get ( " {home}/Profiles " ) ;
2011-06-26 08:30:44 +00:00
QDir dir ( path ) ;
if ( ! dir . exists ( path ) ) {
return ;
}
2014-04-17 05:58:57 +00:00
2011-06-26 08:30:44 +00:00
if ( ! dir . isReadable ( ) ) {
2011-07-01 10:10:44 +00:00
qWarning ( ) < < " Can't open " < < path ;
2011-06-26 08:30:44 +00:00
return ;
}
dir . setFilter ( QDir : : Dirs | QDir : : NoDotAndDotDot ) ;
//dir.setSorting(QDir::Name);
2014-04-17 05:58:57 +00:00
QFileInfoList list = dir . entryInfoList ( ) ;
2011-06-26 08:30:44 +00:00
2011-07-17 07:03:26 +00:00
// Iterate through subdirectories and load profiles..
2014-04-17 05:58:57 +00:00
for ( int i = 0 ; i < list . size ( ) ; i + + ) {
QFileInfo fi = list . at ( i ) ;
QString npath = fi . canonicalFilePath ( ) ;
Profile * prof = new Profile ( npath ) ;
2014-04-25 05:28:10 +00:00
// prof->Open(); // Read it's XML file..
2014-04-17 05:58:57 +00:00
profiles [ fi . fileName ( ) ] = prof ;
2011-06-26 08:30:44 +00:00
}
}
2011-12-06 14:39:14 +00:00
2011-12-18 16:39:36 +00:00
} // namespace Profiles
2011-06-26 08:30:44 +00:00
2011-12-23 10:52:31 +00:00
int Profile : : countDays ( MachineType mt , QDate start , QDate end )
{
2014-04-17 05:58:57 +00:00
if ( ! start . isValid ( ) ) {
2012-01-01 14:14:18 +00:00
return 0 ;
2014-04-17 05:58:57 +00:00
}
2012-01-01 14:14:18 +00:00
//start=LastDay(mt);
2014-04-17 05:58:57 +00:00
if ( ! end . isValid ( ) ) {
2012-01-01 14:14:18 +00:00
return 0 ;
2014-04-17 05:58:57 +00:00
}
2012-01-01 14:14:18 +00:00
//end=LastDay(mt);
2014-04-17 05:58:57 +00:00
QDate date = start ;
if ( date . isNull ( ) ) {
2013-09-11 12:57:06 +00:00
return 0 ;
2014-04-17 05:58:57 +00:00
}
2013-09-11 12:57:06 +00:00
2014-04-17 05:58:57 +00:00
int days = 0 ;
2011-12-23 10:52:31 +00:00
do {
2014-04-17 05:58:57 +00:00
Day * day = GetGoodDay ( date , mt ) ;
2011-12-23 10:52:31 +00:00
if ( day ) {
2014-04-17 05:58:57 +00:00
if ( ( mt = = MT_UNKNOWN ) | | ( day - > machine - > GetType ( ) = = mt ) ) { days + + ; }
2011-12-23 10:52:31 +00:00
}
2014-04-17 05:58:57 +00:00
date = date . addDays ( 1 ) ;
} while ( date < = end ) ;
2011-12-23 10:52:31 +00:00
return days ;
}
2014-05-06 09:11:31 +00:00
int Profile : : countCompliantDays ( MachineType mt , QDate start , QDate end )
{
EventDataType compliance = cpap - > complianceHours ( ) ;
if ( ! start . isValid ( ) ) {
return 0 ;
}
if ( ! end . isValid ( ) ) {
return 0 ;
}
QDate date = start ;
if ( date . isNull ( ) ) {
return 0 ;
}
int days = 0 ;
do {
Day * day = GetGoodDay ( date , mt ) ;
if ( day ) {
if ( ( day - > machine - > GetType ( ) = = mt ) & & ( day - > hours ( ) > compliance ) ) { days + + ; }
}
date = date . addDays ( 1 ) ;
} while ( date < = end ) ;
return days ;
}
2011-12-22 13:22:40 +00:00
EventDataType Profile : : calcCount ( ChannelID code , MachineType mt , QDate start , QDate end )
{
2014-04-17 05:58:57 +00:00
if ( ! start . isValid ( ) ) {
start = LastGoodDay ( mt ) ;
}
if ( ! end . isValid ( ) ) {
end = LastGoodDay ( mt ) ;
}
QDate date = start ;
if ( date . isNull ( ) ) {
2013-09-11 12:57:06 +00:00
return 0 ;
2014-04-17 05:58:57 +00:00
}
double val = 0 ;
2011-12-22 13:22:40 +00:00
do {
2014-04-17 05:58:57 +00:00
Day * day = GetGoodDay ( date , mt ) ;
2011-12-22 13:22:40 +00:00
if ( day ) {
2014-04-17 05:58:57 +00:00
val + = day - > count ( code ) ;
2011-12-22 13:22:40 +00:00
}
2014-04-17 05:58:57 +00:00
date = date . addDays ( 1 ) ;
} while ( date < = end ) ;
2011-12-22 13:22:40 +00:00
return val ;
}
2014-04-22 07:16:24 +00:00
2011-12-22 13:22:40 +00:00
double Profile : : calcSum ( ChannelID code , MachineType mt , QDate start , QDate end )
{
2014-04-17 05:58:57 +00:00
if ( ! start . isValid ( ) ) {
start = LastGoodDay ( mt ) ;
}
if ( ! end . isValid ( ) ) {
end = LastGoodDay ( mt ) ;
}
QDate date = start ;
double val = 0 ;
2011-12-22 13:22:40 +00:00
do {
2014-04-17 05:58:57 +00:00
Day * day = GetGoodDay ( date , mt ) ;
2011-12-22 13:22:40 +00:00
if ( day ) {
2014-04-17 05:58:57 +00:00
val + = day - > sum ( code ) ;
2011-12-22 13:22:40 +00:00
}
2014-04-17 05:58:57 +00:00
date = date . addDays ( 1 ) ;
} while ( date < = end ) ;
2011-12-22 13:22:40 +00:00
return val ;
}
EventDataType Profile : : calcHours ( MachineType mt , QDate start , QDate end )
{
2014-04-17 05:58:57 +00:00
if ( ! start . isValid ( ) ) {
start = LastGoodDay ( mt ) ;
}
if ( ! end . isValid ( ) ) {
end = LastGoodDay ( mt ) ;
}
QDate date = start ;
if ( date . isNull ( ) ) {
2013-09-11 12:57:06 +00:00
return 0 ;
2014-04-17 05:58:57 +00:00
}
double val = 0 ;
2011-12-22 13:22:40 +00:00
do {
2014-04-17 05:58:57 +00:00
Day * day = GetGoodDay ( date , mt ) ;
2011-12-22 13:22:40 +00:00
if ( day ) {
2014-04-17 05:58:57 +00:00
val + = day - > hours ( ) ;
2011-12-22 13:22:40 +00:00
}
2014-04-17 05:58:57 +00:00
date = date . addDays ( 1 ) ;
} while ( date < = end ) ;
2011-12-22 13:22:40 +00:00
return val ;
}
EventDataType Profile : : calcAvg ( ChannelID code , MachineType mt , QDate start , QDate end )
{
2014-04-17 05:58:57 +00:00
if ( ! start . isValid ( ) ) {
start = LastGoodDay ( mt ) ;
}
if ( ! end . isValid ( ) ) {
end = LastGoodDay ( mt ) ;
}
QDate date = start ;
if ( date . isNull ( ) ) {
2013-09-11 12:57:06 +00:00
return 0 ;
2014-04-17 05:58:57 +00:00
}
double val = 0 ;
int cnt = 0 ;
2011-12-22 13:22:40 +00:00
do {
2014-04-17 05:58:57 +00:00
Day * day = GetGoodDay ( date , mt ) ;
2011-12-22 13:22:40 +00:00
if ( day ) {
2014-04-17 05:58:57 +00:00
val + = day - > sum ( code ) ;
2011-12-22 13:22:40 +00:00
cnt + + ;
}
2014-04-17 05:58:57 +00:00
date = date . addDays ( 1 ) ;
} while ( date < = end ) ;
if ( ! cnt ) {
2013-09-11 12:57:06 +00:00
return 0 ;
2014-04-17 05:58:57 +00:00
}
return val / float ( cnt ) ;
2011-12-22 13:22:40 +00:00
}
2014-04-22 07:16:24 +00:00
2011-12-22 13:22:40 +00:00
EventDataType Profile : : calcWavg ( ChannelID code , MachineType mt , QDate start , QDate end )
{
2014-04-17 05:58:57 +00:00
if ( ! start . isValid ( ) ) {
start = LastGoodDay ( mt ) ;
}
if ( ! end . isValid ( ) ) {
end = LastGoodDay ( mt ) ;
}
QDate date = start ;
if ( date . isNull ( ) ) {
2013-09-11 12:57:06 +00:00
return 0 ;
2014-04-17 05:58:57 +00:00
}
double val = 0 , tmp , tmph , hours = 0 ;
2011-12-22 13:22:40 +00:00
do {
2014-04-17 05:58:57 +00:00
Day * day = GetGoodDay ( date , mt ) ;
2011-12-22 13:22:40 +00:00
if ( day ) {
2014-04-17 05:58:57 +00:00
tmph = day - > hours ( ) ;
tmp = day - > wavg ( code ) ;
val + = tmp * tmph ;
hours + = tmph ;
}
date = date . addDays ( 1 ) ;
} while ( date < = end ) ;
if ( ! hours ) {
2013-09-11 12:57:06 +00:00
return 0 ;
2014-04-17 05:58:57 +00:00
}
val = val / hours ;
2011-12-22 13:22:40 +00:00
return val ;
}
2014-04-22 07:16:24 +00:00
2011-12-23 10:52:31 +00:00
EventDataType Profile : : calcMin ( ChannelID code , MachineType mt , QDate start , QDate end )
{
2014-04-17 05:58:57 +00:00
if ( ! start . isValid ( ) ) {
start = LastGoodDay ( mt ) ;
}
if ( ! end . isValid ( ) ) {
end = LastGoodDay ( mt ) ;
}
QDate date = start ;
if ( date . isNull ( ) ) {
2013-09-11 12:57:06 +00:00
return 0 ;
2014-04-17 05:58:57 +00:00
}
2013-09-11 12:57:06 +00:00
2011-12-23 10:52:31 +00:00
2014-04-17 05:58:57 +00:00
double min = 99999999 , tmp ;
2011-12-23 10:52:31 +00:00
do {
2014-04-17 05:58:57 +00:00
Day * day = GetGoodDay ( date , mt ) ;
2011-12-23 10:52:31 +00:00
if ( day ) {
2014-04-17 05:58:57 +00:00
tmp = day - > Min ( code ) ;
if ( min > tmp ) {
min = tmp ;
}
}
date = date . addDays ( 1 ) ;
} while ( date < = end ) ;
if ( min > = 99999999 ) {
min = 0 ;
}
2011-12-23 10:52:31 +00:00
return min ;
}
EventDataType Profile : : calcMax ( ChannelID code , MachineType mt , QDate start , QDate end )
{
2014-04-17 05:58:57 +00:00
if ( ! start . isValid ( ) ) {
start = LastGoodDay ( mt ) ;
}
if ( ! end . isValid ( ) ) {
end = LastGoodDay ( mt ) ;
}
QDate date = start ;
double max = - 99999999 , tmp ;
2011-12-23 10:52:31 +00:00
do {
2014-04-17 05:58:57 +00:00
Day * day = GetGoodDay ( date , mt ) ;
2011-12-23 10:52:31 +00:00
if ( day ) {
2014-04-17 05:58:57 +00:00
tmp = day - > Max ( code ) ;
if ( max < tmp ) {
max = tmp ;
}
}
date = date . addDays ( 1 ) ;
} while ( date < = end ) ;
if ( max < = - 99999999 ) {
max = 0 ;
}
2011-12-23 10:52:31 +00:00
return max ;
}
EventDataType Profile : : calcSettingsMin ( ChannelID code , MachineType mt , QDate start , QDate end )
{
2014-04-17 05:58:57 +00:00
if ( ! start . isValid ( ) ) {
start = LastGoodDay ( mt ) ;
}
if ( ! end . isValid ( ) ) {
end = LastGoodDay ( mt ) ;
}
QDate date = start ;
if ( date . isNull ( ) ) {
2013-09-11 12:57:06 +00:00
return 0 ;
2014-04-17 05:58:57 +00:00
}
double min = 99999999 , tmp ;
2011-12-23 10:52:31 +00:00
do {
2014-04-17 05:58:57 +00:00
Day * day = GetGoodDay ( date , mt ) ;
2011-12-23 10:52:31 +00:00
if ( day ) {
2014-04-17 05:58:57 +00:00
tmp = day - > settings_min ( code ) ;
if ( min > tmp ) {
min = tmp ;
}
}
date = date . addDays ( 1 ) ;
} while ( date < = end ) ;
if ( min > = 99999999 ) {
min = 0 ;
}
2011-12-23 10:52:31 +00:00
return min ;
}
EventDataType Profile : : calcSettingsMax ( ChannelID code , MachineType mt , QDate start , QDate end )
{
2014-04-17 05:58:57 +00:00
if ( ! start . isValid ( ) ) {
start = LastGoodDay ( mt ) ;
}
if ( ! end . isValid ( ) ) {
end = LastGoodDay ( mt ) ;
}
QDate date = start ;
if ( date . isNull ( ) ) {
2013-09-11 12:57:06 +00:00
return 0 ;
2014-04-17 05:58:57 +00:00
}
double max = - 99999999 , tmp ;
2011-12-23 10:52:31 +00:00
do {
2014-04-17 05:58:57 +00:00
Day * day = GetGoodDay ( date , mt ) ;
2011-12-23 10:52:31 +00:00
if ( day ) {
2014-04-17 05:58:57 +00:00
tmp = day - > settings_max ( code ) ;
if ( max < tmp ) {
max = tmp ;
}
}
date = date . addDays ( 1 ) ;
} while ( date < = end ) ;
if ( max < = - 99999999 ) {
max = 0 ;
}
2011-12-23 10:52:31 +00:00
return max ;
}
2011-12-24 01:22:41 +00:00
struct CountSummary {
2014-04-17 05:58:57 +00:00
CountSummary ( EventStoreType v ) : val ( v ) , count ( 0 ) , time ( 0 ) { }
2011-12-24 01:22:41 +00:00
EventStoreType val ;
EventStoreType count ;
quint32 time ;
} ;
2014-04-17 05:58:57 +00:00
EventDataType Profile : : calcPercentile ( ChannelID code , EventDataType percent , MachineType mt ,
QDate start , QDate end )
2011-12-22 13:22:40 +00:00
{
2014-04-17 05:58:57 +00:00
if ( ! start . isValid ( ) ) {
start = LastGoodDay ( mt ) ;
}
if ( ! end . isValid ( ) ) {
end = LastGoodDay ( mt ) ;
}
QDate date = start ;
2011-12-22 13:22:40 +00:00
2014-04-17 05:58:57 +00:00
if ( date . isNull ( ) ) {
2013-09-11 12:57:06 +00:00
return 0 ;
2014-04-17 05:58:57 +00:00
}
2011-12-22 13:22:40 +00:00
2012-02-01 14:53:31 +00:00
QMap < EventDataType , qint64 > wmap ;
2013-10-13 10:13:14 +00:00
QMap < EventDataType , qint64 > : : iterator wmi ;
2011-12-24 01:22:41 +00:00
2014-04-17 05:58:57 +00:00
QHash < ChannelID , QHash < EventStoreType , EventStoreType > > : : iterator vsi ;
QHash < ChannelID , QHash < EventStoreType , quint32 > > : : iterator tsi ;
2012-01-05 12:12:42 +00:00
EventDataType gain ;
//bool setgain=false;
2012-02-01 14:53:31 +00:00
EventDataType value ;
int weight ;
2012-01-01 14:14:18 +00:00
2014-04-17 05:58:57 +00:00
qint64 SN = 0 ;
2012-02-01 14:53:31 +00:00
bool timeweight ;
2014-04-17 05:58:57 +00:00
2011-12-22 13:22:40 +00:00
do {
2014-04-17 05:58:57 +00:00
Day * day = GetGoodDay ( date , mt ) ;
2011-12-23 10:52:31 +00:00
if ( day ) {
2014-04-17 05:58:57 +00:00
for ( int i = 0 ; i < day - > size ( ) ; i + + ) {
for ( QList < Session * > : : iterator s = day - > begin ( ) ; s ! = day - > end ( ) ; s + + ) {
if ( ! ( * s ) - > enabled ( ) ) {
2012-01-01 14:14:18 +00:00
continue ;
2014-04-17 05:58:57 +00:00
}
Session * sess = * s ;
gain = sess - > m_gain [ code ] ;
if ( ! gain ) { gain = 1 ; }
2011-12-28 12:03:48 +00:00
2014-04-17 05:58:57 +00:00
vsi = sess - > m_valuesummary . find ( code ) ;
2011-12-24 01:22:41 +00:00
2014-04-17 05:58:57 +00:00
if ( vsi = = sess - > m_valuesummary . end ( ) ) { continue ; }
tsi = sess - > m_timesummary . find ( code ) ;
timeweight = ( tsi ! = sess - > m_timesummary . end ( ) ) ;
QHash < EventStoreType , EventStoreType > & vsum = vsi . value ( ) ;
QHash < EventStoreType , quint32 > & tsum = tsi . value ( ) ;
2012-02-01 14:53:31 +00:00
if ( timeweight ) {
2014-04-17 05:58:57 +00:00
for ( QHash < EventStoreType , quint32 > : : iterator k = tsum . begin ( ) ; k ! = tsum . end ( ) ; k + + ) {
weight = k . value ( ) ;
value = EventDataType ( k . key ( ) ) * gain ;
SN + = weight ;
wmi = wmap . find ( value ) ;
if ( wmi = = wmap . end ( ) ) {
wmap [ value ] = weight ;
2013-10-13 10:13:14 +00:00
} else {
2014-04-17 05:58:57 +00:00
wmi . value ( ) + = weight ;
2012-02-01 14:53:31 +00:00
}
}
} else {
2014-04-17 05:58:57 +00:00
for ( QHash < EventStoreType , EventStoreType > : : iterator k = vsum . begin ( ) ; k ! = vsum . end ( ) ; k + + ) {
weight = k . value ( ) ;
value = EventDataType ( k . key ( ) ) * gain ;
SN + = weight ;
wmi = wmap . find ( value ) ;
if ( wmi = = wmap . end ( ) ) {
wmap [ value ] = weight ;
2013-10-13 10:13:14 +00:00
} else {
2014-04-17 05:58:57 +00:00
wmi . value ( ) + = weight ;
2012-02-01 14:53:31 +00:00
}
2011-12-24 01:22:41 +00:00
}
}
2011-12-23 10:52:31 +00:00
}
}
}
2014-04-17 05:58:57 +00:00
date = date . addDays ( 1 ) ;
} while ( date < = end ) ;
2012-01-05 04:37:22 +00:00
QVector < ValueCount > valcnt ;
// Build sorted list of value/counts
2014-04-17 05:58:57 +00:00
for ( wmi = wmap . begin ( ) ; wmi ! = wmap . end ( ) ; wmi + + ) {
2012-01-05 04:37:22 +00:00
ValueCount vc ;
2014-04-17 05:58:57 +00:00
vc . value = wmi . key ( ) ;
vc . count = wmi . value ( ) ;
vc . p = 0 ;
2012-01-05 04:37:22 +00:00
valcnt . push_back ( vc ) ;
}
2014-04-17 05:58:57 +00:00
2012-01-05 04:37:22 +00:00
// sort by weight, then value
qSort ( valcnt ) ;
2011-12-24 01:22:41 +00:00
2012-01-05 04:37:22 +00:00
//double SN=100.0/double(N); // 100% / overall sum
2014-04-17 05:58:57 +00:00
double p = 100.0 * percent ;
2011-12-24 05:55:44 +00:00
2014-04-17 05:58:57 +00:00
double nth = double ( SN ) * percent ; // index of the position in the unweighted set would be
double nthi = floor ( nth ) ;
2011-12-24 05:55:44 +00:00
2014-04-17 05:58:57 +00:00
qint64 sum1 = 0 , sum2 = 0 ;
qint64 w1 , w2 = 0 ;
double v1 = 0 , v2 = 0 ;
2011-12-24 05:55:44 +00:00
2014-04-17 05:58:57 +00:00
int N = valcnt . size ( ) ;
int k = 0 ;
2011-12-24 05:55:44 +00:00
2014-04-17 05:58:57 +00:00
for ( k = 0 ; k < N ; k + + ) {
v1 = valcnt [ k ] . value ;
w1 = valcnt [ k ] . count ;
sum1 + = w1 ;
2012-01-05 04:37:22 +00:00
if ( sum1 > nthi ) {
return v1 ;
}
2014-04-17 05:58:57 +00:00
if ( sum1 = = nthi ) {
2012-01-05 04:37:22 +00:00
break ; // boundary condition
}
}
2014-04-17 05:58:57 +00:00
if ( k > = N ) {
2012-01-05 04:37:22 +00:00
return v1 ;
2014-04-17 05:58:57 +00:00
}
2012-01-05 04:37:22 +00:00
2014-04-17 05:58:57 +00:00
v2 = valcnt [ k + 1 ] . value ;
w2 = valcnt [ k + 1 ] . count ;
sum2 = sum1 + w2 ;
2012-01-05 04:37:22 +00:00
// value lies between v1 and v2
2014-04-17 05:58:57 +00:00
double px = 100.0 / double ( SN ) ; // Percentile represented by one full value
2012-01-05 04:37:22 +00:00
// calculate percentile ranks
2014-04-17 05:58:57 +00:00
double p1 = px * ( double ( sum1 ) - ( double ( w1 ) / 2.0 ) ) ;
double p2 = px * ( double ( sum2 ) - ( double ( w2 ) / 2.0 ) ) ;
2012-01-05 04:37:22 +00:00
// calculate linear interpolation
2014-04-17 05:58:57 +00:00
double v = v1 + ( ( p - p1 ) / ( p2 - p1 ) ) * ( v2 - v1 ) ;
2011-12-24 05:55:44 +00:00
2012-01-05 04:37:22 +00:00
// p1.....p.............p2
// 37 55 70
return v ;
2011-12-22 13:22:40 +00:00
}
2013-09-14 23:32:14 +00:00
// Lookup first day record of the specified machine type, or return the first day overall if MT_UNKNOWN
2011-12-22 13:22:40 +00:00
QDate Profile : : FirstDay ( MachineType mt )
{
2014-04-17 05:58:57 +00:00
if ( ( mt = = MT_UNKNOWN ) | | ( ! m_last . isValid ( ) ) | | ( ! m_first . isValid ( ) ) ) {
2011-12-22 13:22:40 +00:00
return m_first ;
2014-04-17 05:58:57 +00:00
}
QDate d = m_first ;
2011-12-22 13:22:40 +00:00
do {
2014-04-23 13:19:56 +00:00
if ( GetDay ( d , mt ) ! = nullptr ) {
2013-09-11 12:57:06 +00:00
return d ;
2014-04-17 05:58:57 +00:00
}
d = d . addDays ( 1 ) ;
} while ( d < = m_last ) ;
2011-12-22 13:22:40 +00:00
return m_last ;
}
2013-09-14 23:32:14 +00:00
// Lookup last day record of the specified machine type, or return the first day overall if MT_UNKNOWN
2011-12-22 13:22:40 +00:00
QDate Profile : : LastDay ( MachineType mt )
{
2014-04-17 05:58:57 +00:00
if ( ( mt = = MT_UNKNOWN ) | | ( ! m_last . isValid ( ) ) | | ( ! m_first . isValid ( ) ) ) {
2011-12-22 13:22:40 +00:00
return m_last ;
2014-04-17 05:58:57 +00:00
}
QDate d = m_last ;
2011-12-22 13:22:40 +00:00
do {
2014-04-23 13:19:56 +00:00
if ( GetDay ( d , mt ) ! = nullptr ) {
2013-09-11 12:57:06 +00:00
return d ;
2014-04-17 05:58:57 +00:00
}
d = d . addDays ( - 1 ) ;
} while ( d > = m_first ) ;
2011-12-22 13:22:40 +00:00
return m_first ;
}
2011-12-28 12:36:40 +00:00
QDate Profile : : FirstGoodDay ( MachineType mt )
{
2014-04-17 05:58:57 +00:00
if ( mt = = MT_UNKNOWN ) { //|| (!m_last.isValid()) || (!m_first.isValid()))
2011-12-28 12:36:40 +00:00
return FirstDay ( ) ;
2014-04-17 05:58:57 +00:00
}
2011-12-28 12:36:40 +00:00
2014-04-17 05:58:57 +00:00
QDate d = FirstDay ( mt ) ;
QDate l = LastDay ( mt ) ;
if ( ! d . isValid ( ) | | ! l . isValid ( ) ) {
2012-01-01 14:14:18 +00:00
return QDate ( ) ;
2014-04-17 05:58:57 +00:00
}
2011-12-28 12:36:40 +00:00
do {
2014-04-23 13:19:56 +00:00
if ( GetGoodDay ( d , mt ) ! = nullptr ) {
2013-09-11 12:57:06 +00:00
return d ;
2014-04-17 05:58:57 +00:00
}
d = d . addDays ( 1 ) ;
} while ( d < = l ) ;
2011-12-28 12:36:40 +00:00
return l ; //m_last;
}
QDate Profile : : LastGoodDay ( MachineType mt )
{
2014-04-17 05:58:57 +00:00
if ( mt = = MT_UNKNOWN ) { //|| (!m_last.isValid()) || (!m_first.isValid()))
2011-12-28 12:36:40 +00:00
return FirstDay ( ) ;
2014-04-17 05:58:57 +00:00
}
QDate d = LastDay ( mt ) ;
QDate f = FirstDay ( mt ) ;
if ( ! ( d . isValid ( ) & & f . isValid ( ) ) ) {
2013-09-11 12:57:06 +00:00
return QDate ( ) ;
2014-04-17 05:58:57 +00:00
}
2011-12-28 12:36:40 +00:00
do {
2014-04-23 13:19:56 +00:00
if ( GetGoodDay ( d , mt ) ! = nullptr ) {
2013-09-11 12:57:06 +00:00
return d ;
2014-04-17 05:58:57 +00:00
}
d = d . addDays ( - 1 ) ;
} while ( d > = f ) ;
2011-12-28 12:36:40 +00:00
return f ; //m_first;
}
2012-01-25 22:34:21 +00:00
bool Profile : : hasChannel ( ChannelID code )
{
2014-04-17 05:58:57 +00:00
QDate d = LastDay ( ) ;
QDate f = FirstDay ( ) ;
if ( ! ( d . isValid ( ) & & f . isValid ( ) ) ) {
2013-09-11 12:57:06 +00:00
return false ;
2014-04-17 05:58:57 +00:00
}
QMap < QDate , QList < Day * > > : : iterator dit ;
2014-04-22 07:16:24 +00:00
QList < Day * > : : iterator di ;
QList < Day * > : : iterator di_end ;
2014-04-17 05:58:57 +00:00
bool found = false ;
2012-01-25 22:34:21 +00:00
do {
2014-04-17 05:58:57 +00:00
dit = daylist . find ( d ) ;
if ( dit ! = daylist . end ( ) ) {
2014-04-22 07:16:24 +00:00
di = dit . value ( ) . begin ( ) ;
di_end = dit . value ( ) . end ( ) ;
for ( ; di ! = di_end ; + + di ) {
Day * day = ( * di ) ;
2014-04-17 05:58:57 +00:00
2012-01-25 22:34:21 +00:00
if ( day - > channelHasData ( code ) ) {
2014-04-17 05:58:57 +00:00
found = true ;
2012-01-25 22:34:21 +00:00
break ;
}
}
}
2014-04-17 05:58:57 +00:00
if ( found ) {
2013-09-11 12:57:06 +00:00
break ;
2014-04-17 05:58:57 +00:00
}
d = d . addDays ( - 1 ) ;
} while ( d > = f ) ;
2012-01-25 22:34:21 +00:00
return found ;
}