summaryrefslogtreecommitdiffstats
path: root/krecipes/src/dialogs/dietviewdialog.cpp
blob: 5b51c27fd85b92f00046e6e09c5f026fb84d8feb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/***************************************************************************
*   Copyright (C) 2003-2005                                               *
*   Unai Garro (ugarro@users.sourceforge.net)                             *
*   Jason Kivlighn (jkivlighn@gmail.com)                                  *
*                                                                         *
*   This program is free software; you can redistribute it and/or modify  *
*   it under the terms of the GNU General Public License as published by  *
*   the Free Software Foundation; either version 2 of the License, or     *
*   (at your option) any later version.                                   *
***************************************************************************/

#include "dietviewdialog.h"

#include <kiconloader.h>
#include <tdelocale.h>
#include <kstandarddirs.h>

DietViewDialog::DietViewDialog( TQWidget *parent, const RecipeList &recipeList, int dayNumber, int mealNumber, const TQValueList <int> &dishNumbers )
		: KDialogBase( parent, "dietViewDialog", true, TQString::null,
		    KDialogBase::User2 | KDialogBase::Close | KDialogBase::User1, KDialogBase::User2,
		    false, KStdGuiItem::print() )
{
	setButtonText( KDialogBase::User2, i18n( "Create &Shopping List" ) );

	// Design the dialog
	TQVBox *page = makeVBoxMainWidget();

	// The html part
	dietView = new TDEHTMLPart( page );

	setInitialSize( TQSize(350, 450) );

	setSizeGripEnabled( true );

	connect ( this, TQ_SIGNAL( user2Clicked() ), this, TQ_SLOT( slotOk() ) );
 	connect ( this, TQ_SIGNAL( closeClicked() ), this, TQ_SLOT( close() ) );
	connect ( this, TQ_SIGNAL( user1Clicked() ), this, TQ_SLOT( print() ) );

	// Show the diet
	showDiet( recipeList, dayNumber, mealNumber, dishNumbers );
}

DietViewDialog::~DietViewDialog()
{}

void DietViewDialog::showDiet( const RecipeList &recipeList, int dayNumber, int mealNumber, const TQValueList <int> &dishNumbers )
{


	// Header
	TQString htmlCode = TQString( "<html><head><title>%1</title>" ).arg( i18n( "Diet" ) );

	// CSS
	htmlCode += "<STYLE type=\"text/css\">\n";
	htmlCode += "#calendar{border: thin solid black}";
	htmlCode += ".dayheader{ background-color: #D6D6D6; color: black; border:none;}";
	htmlCode += ".day{ background-color: #E5E5E5; color: black; border:medium solid #D6D6D6;}";
	htmlCode += ".meal{ background-color: #CDD4FF; color: black; border:thin solid #B4BEFF; text-align:center;}";
	htmlCode += ".dish{font-size: smaller; overflow: hidden; height:2.5em;}";
	htmlCode += "</STYLE>";


	htmlCode += "</head><body>"; //  /Header

	// Calendar border
	htmlCode += TQString( "<div id=\"calendar\">" );

	// Title
	htmlCode += TQString( "<center><div STYLE=\"width: 100%\">" );
	htmlCode += TQString( "<h1>%1</h1></div></center>" ).arg( i18n( "Diet" ) );

	// Diet table
	htmlCode += TQString( "<center><div STYLE=\"width: 98%\">" );
	htmlCode += TQString( "<table><tbody>" );


	TQValueList <int>::ConstIterator it;
	it = dishNumbers.begin();
	RecipeList::ConstIterator rit;
	rit = recipeList.begin();

	for ( int row = 0, day = 0; row <= ( ( dayNumber - 1 ) / 7 ); row++ )  // New row (week)
	{
		htmlCode += TQString( "<tr>" );

		for ( int col = 0; ( col < 7 ) && ( day < dayNumber ); col++, day++ )  // New column (day)
		{
			htmlCode += TQString( "<td><div class=\"day\">" );
			htmlCode += TQString( "<div class=\"dayheader\"><center>" );
			htmlCode += TQString( i18n( "Day %1" ) ).arg( day + 1 );
			htmlCode += TQString( "</center></div>" );
			for ( int meal = 0;meal < mealNumber;meal++ )  // Meals in each cell
			{
				int dishNumber = *it;
				htmlCode += TQString( "<div class=\"meal\">" );
				for ( int dish = 0; dish < dishNumber;dish++ )  // Dishes in each Meal
				{
					htmlCode += TQString( "<div class=\"dish\">" );
					htmlCode += ( *rit ).title;
					htmlCode += "<br>";
					htmlCode += TQString( "</div>" );
					rit++;
				}
				it++;
				htmlCode += TQString( "</div>" );
			}
			it = dishNumbers.begin(); // meals have same dish number everyday
			htmlCode += TQString( "</div></td>" );
		}

		htmlCode += TQString( "</tr>" );
	}

	htmlCode += TQString( "</tbody></table>" );
	htmlCode += TQString( "</div></center>" );
	htmlCode += TQString( "</div></body></html>" );

	resize( TQSize( 600, 400 ) );

	// Display it
	dietView->begin( KURL( locateLocal( "tmp", "/" ) ) ); // Initialize to tmp dir, where photos and logos can be stored
	dietView->write( htmlCode );
	dietView->end();
}

void DietViewDialog::print( void )
{
	dietView->view()->print();
}

void DietViewDialog::slotOk( void )
{
	emit signalOk();
	close();
}

#include "dietviewdialog.moc"