Skip to content

Commit

Permalink
bugfix: WeekTime object to calculate the total time in one week.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmo16 committed May 3, 2020
1 parent d277323 commit 04f9d98
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 8 deletions.
53 changes: 53 additions & 0 deletions src/Model/Timesheet/WeekTime.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include "WeekTime.h"

#include <QObject>
#include <QString>

namespace Model
{
WeekTime::WeekTime( QObject* pParent_ ) : QObject( pParent_ ) {}

bool WeekTime::addTime( int timeMs_ )
{
if ( timeMs_ > INT_MAX || ( m_totalTimeMs + timeMs_ ) > INT_MAX )
{
return false;
}

m_totalTimeMs += timeMs_;
return true;
}

int WeekTime::getTime() const
{
return m_totalTimeMs;
}

void WeekTime::reset()
{
m_totalTimeMs = 0;
}

QString weekTimeToString( const WeekTime& weekTime_ )
{
const int msInHour = 1000 * 60 * 60;
const int msInMinutes = 1000 * 60;

const int& timeMs = weekTime_.getTime();
const int hours = timeMs / msInHour;
int rest = timeMs - ( hours * msInHour );

const int minutes = rest / msInMinutes;

QString timeString = QString::number( hours ) + "h" + QString::number( minutes );

const QStringList& timeStrings = timeString.split( "h" );
if ( timeStrings.size() > 1 && timeStrings[ 1 ] == "0" )
{
timeString.append( "0" );
}

return timeString;
}

} // namespace Model
27 changes: 27 additions & 0 deletions src/Model/Timesheet/WeekTime.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

#include <QObject>
#include <QString>

namespace Model
{
class WeekTime : public QObject
{
Q_OBJECT

public:
WeekTime( QObject* pParent_ = Q_NULLPTR );
~WeekTime() = default;

bool addTime( int timeMs_ );

int getTime() const;

void reset();

private:
int m_totalTimeMs = 0;
};

QString weekTimeToString( const WeekTime& weekTime_ );
} // namespace Model
3 changes: 3 additions & 0 deletions src/TimesheetAssistant.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ $(QTDIR)\bin\windeployqt.exe $(OutDir)</Command>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="Model\Config\Config.cpp" />
<ClCompile Include="Model\Timesheet\WeekTime.cpp" />
<ClCompile Include="View\Components\SettingsDialog.cpp" />
<ClCompile Include="View\Components\AboutDialog.cpp" />
<ClCompile Include="View\Components\HelpDialog.cpp" />
Expand Down Expand Up @@ -167,6 +168,7 @@ $(QTDIR)\bin\windeployqt.exe $(OutDir)</Command>
<ItemGroup>
<None Include="..\.clang-format" />
<None Include="..\README.md" />
<None Include="cpp.hint" />
<None Include="Resources\rt_manif.bin" />
<None Include="Resources\TimesheetAssistant.aps" />
</ItemGroup>
Expand All @@ -183,6 +185,7 @@ $(QTDIR)\bin\windeployqt.exe $(OutDir)</Command>
<QtMoc Include="Model\Timesheet\WorkDay.h" />
</ItemGroup>
<ItemGroup>
<QtMoc Include="Model\Timesheet\WeekTime.h" />
<QtMoc Include="View\Components\SettingsDialog.h" />
<QtMoc Include="View\Components\HelpDialog.h" />
<QtMoc Include="View\Components\AboutDialog.h" />
Expand Down
7 changes: 7 additions & 0 deletions src/TimesheetAssistant.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@
<ClCompile Include="Model\Config\Config.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Model\Timesheet\WeekTime.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<Image Include="Resources\TimesheetAssistant.ico">
Expand Down Expand Up @@ -97,6 +100,9 @@
<QtMoc Include="Model\Config\Config.h">
<Filter>Header Files</Filter>
</QtMoc>
<QtMoc Include="Model\Timesheet\WeekTime.h">
<Filter>Header Files</Filter>
</QtMoc>
</ItemGroup>
<ItemGroup>
<QtUic Include="View\gui\HelpDialog.ui">
Expand All @@ -117,6 +123,7 @@
<None Include="..\README.md" />
<None Include="Resources\TimesheetAssistant.aps" />
<None Include="Resources\rt_manif.bin" />
<None Include="cpp.hint" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Model\Utils\NotImplemented.h">
Expand Down
13 changes: 6 additions & 7 deletions src/View/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "Model/Events/Event.h"
#include "Model/ModelThread.h"
#include "Model/Timesheet/Timesheet.h"
#include "Model/Timesheet/WeekTime.h"
#include "Model/Timesheet/WorkDay.h"

#include <QAction>
Expand All @@ -16,8 +17,7 @@ namespace View
MainWindow::MainWindow(
std::unique_ptr<Model::ModelThread> pModelThread_, QSharedPointer<Model::Config> pConfig_, QWidget* pParent_ )
: QMainWindow( pParent_ ), m_pModelThread( std::move( pModelThread_ ) ), m_helpDialog( this ),
m_aboutDialog( this ), m_settingsDialog( pConfig_, this ), m_currentDayTotalTime( QTime( 0, 0 ) ),
m_totalWorkTime( QTime( 0, 0 ) )
m_aboutDialog( this ), m_settingsDialog( pConfig_, this ), m_currentDayTotalTime( QTime( 0, 0 ) )
{
m_ui.setupUi( this );

Expand Down Expand Up @@ -53,7 +53,7 @@ namespace View

pWorkDayLineEdit->setText( m_workDaysStrings.at( index ) );
}
m_ui.totalTime->setText( m_totalWorkTime.toString( "H'h'mm" ) );
m_ui.totalTime->setText( Model::weekTimeToString( m_totalWorkTime ) );
}

void MainWindow::onRefreshClicked()
Expand All @@ -74,7 +74,6 @@ namespace View
m_workDaysStrings[ index ] = m_currentDayTotalTime.toString( "H'h'mm" );

calculateTotalTime();

update();
}

Expand Down Expand Up @@ -156,11 +155,11 @@ namespace View

void MainWindow::calculateTotalTime()
{
QTime totalTime( 0, 0 );
m_totalWorkTime.reset();

for ( auto& workdayString : m_workDaysStrings )
{
totalTime = totalTime.addMSecs( QTime::fromString( workdayString, "H'h'mm" ).msecsSinceStartOfDay() );
m_totalWorkTime.addTime( QTime::fromString( workdayString, "H'h'mm" ).msecsSinceStartOfDay() );
}
m_totalWorkTime = totalTime;
}
} // namespace View
3 changes: 2 additions & 1 deletion src/View/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "Components/SettingsDialog.h"
#include "Model/Config/Config.h"
#include "Model/Events/Event.h"
#include "Model/Timesheet/WeekTime.h"

#include <QDate>
#include <QElapsedTimer>
Expand Down Expand Up @@ -62,7 +63,7 @@ namespace View
QDate m_mondayDate;
QDate m_fridayDate;
QTime m_currentDayTotalTime;
QTime m_totalWorkTime;
Model::WeekTime m_totalWorkTime;
QElapsedTimer m_currentDayTimer;
};

Expand Down
4 changes: 4 additions & 0 deletions src/cpp.hint
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Hint files help the Visual Studio IDE interpret Visual C++ identifiers
// such as names of functions and macros.
// For more information see https://go.microsoft.com/fwlink/?linkid=865984
#define INT_MAX

0 comments on commit 04f9d98

Please sign in to comment.