summaryrefslogtreecommitdiffstats
path: root/kghostview/displayoptions.cpp
blob: 6e4dddc71531922df17d5a6e10d6362360cac38d (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
138
139
140
141
142
143
144
145
146
147
148
149
150
/**
 * Copyright (C) 2003, Lu�s Pedro Coelho
 *
 * 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.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
#include "displayoptions.h"
#include <kdebug.h>
#include <tqregexp.h>
#include <kcmdlineargs.h>
#include <kconfig.h>

namespace {
	const double allowedMagnifications[] = {
		0.125,
		0.25,
		0.3333,
		0.5,
		0.6667,
		0.75,
		1,
		1.25,
		1.50,
		2,
		3,
		4,
		6,
		8
	};
	const size_t numberOfMagnifications = ( sizeof( allowedMagnifications ) / sizeof( allowedMagnifications[ 0 ] ) );
}

DisplayOptions DisplayOptions::parse( TDECmdLineArgs* args )
{
	DisplayOptions res;
#define CHECK_ORIENTATION( tag, value ) \
	if ( args->isSet( tag ) ) res._overrideOrientation = value; \
	if ( args->getOption( "orientation" ) == tag ) res._overrideOrientation = value;

	CHECK_ORIENTATION( "landscape", CDSC_LANDSCAPE );
	CHECK_ORIENTATION( "seascape", CDSC_SEASCAPE );
	CHECK_ORIENTATION( "portrait", CDSC_PORTRAIT);
	CHECK_ORIENTATION( "upsidedown", CDSC_UPSIDEDOWN);

	res.setMagnification( args->getOption( "scale" ).toFloat() );
	res.setPage( args->getOption( "page" ).toInt() - 1 ); // transform from 1-based into 0-based
	//res._overridePageMedia = args->getOption( "paper" );
	kdDebug(4500 ) << "Parsed options: " << DisplayOptions::toString( res ) << endl;
	return res;
}

namespace {
	const char* const rformat = ".page: (\\d+); .magnification: ([\\d\\.]+); .orientation = (\\d+); .media = ([^;]*);";
	const char* const qformat = ".page: %1; .magnification: %2; .orientation = %3; .media = %4;";
}

TQString DisplayOptions::toString( const DisplayOptions& options )
{
	return TQString( qformat )
		.arg( options.page() )
		.arg( options.magnification() )
		.arg( options.overrideOrientation() )
		.arg( options.overridePageMedia().utf8().data() );
}

bool DisplayOptions::fromString( DisplayOptions& out, const TQString& in )
{
	TQRegExp regex( TQString::fromLatin1( rformat ) );
	if ( regex.search( in ) < 0 ) return false;

	out.reset();
	out.setPage( regex.cap( 1 ).toInt() );
	out.setMagnification( regex.cap( 2 ).toDouble() );
	out.setOverrideOrientation( static_cast<CDSC_ORIENTATION_ENUM>( regex.cap( 3 ).toInt() ) );
	if ( !regex.cap( 4 ).isEmpty() ) out.setOverridePageMedia( regex.cap( 4 ) );
	return true;
}


bool DisplayOptions::canZoomIn() const
{
	return unsigned( closestIndex() ) < ( numberOfMagnifications - 1 );
}

bool DisplayOptions::zoomIn()
{
	if ( !canZoomIn() ) return false;
	_magnification = allowedMagnifications[ closestIndex() + 1 ];
	return true;
}

bool DisplayOptions::canZoomOut() const
{
	return closestIndex() > 0;
}

bool DisplayOptions::zoomOut()
{
	if ( !canZoomOut() ) return false;
	_magnification = allowedMagnifications[ closestIndex() - 1 ];
	return true;
}

double DisplayOptions::magnification() const
{
	return _magnification;
}


void DisplayOptions::setMagnification( double newZoom ) {
	_magnification = newZoom;
}

unsigned DisplayOptions::closestIndex() const {
	kdDebug(4500) << "DisplayOptions::closestIndex(" << _magnification << ")" << endl;
	unsigned res = 0;
	while ( res < numberOfMagnifications
			&& allowedMagnifications[ res ] < _magnification ) ++res;
	if ( res >= ( numberOfMagnifications - 1 ) ) return numberOfMagnifications - 1;
	if ( res == 0 ) return 0;

	if ( ( allowedMagnifications[ res ] - _magnification ) > ( _magnification - allowedMagnifications[ res - 1 ] ) ) {
		--res;
	}
	kdDebug(4500) << "DisplayOptions::closestIndex(" << res << "): nearest allowed magnification: "
		<< allowedMagnifications[ res ] << endl;
	return res;
}

TQValueList<double> DisplayOptions::normalMagnificationValues() {
	TQValueList<double> res;
	for ( const double *first = allowedMagnifications, *last = allowedMagnifications + numberOfMagnifications;
			first != last;
			++first ) {
		res << *first;
	}
	return res;
}