• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdegames API Reference
  • KDE Home
  • Contact Us
 

palapeli/libpala

  • sources
  • kde-4.14
  • kdegames
  • palapeli
  • libpala
slicerpropertyset.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright 2009 Stefan Majewsky <majewsky@gmx.net>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library 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 program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  ***************************************************************************/
18 
19 #include "slicerpropertyset.h"
20 #include "slicer.h"
21 #include "slicerjob.h"
22 #include "slicerproperty.h"
23 
24 #include <cmath>
25 #include <QDebug>
26 #include <KLocalizedString>
27 
28 //BEGIN Private classes
29 
30 struct Pala::SlicerPropertySet::Private
31 {
32  Pala::Slicer* m_slicer;
33 };
34 
35 struct Pala::SimpleGridPropertySet::Private {};
36 
37 //END Private classes
38 
39 //BEGIN Pala::SlicerPropertySet
40 
41 Pala::SlicerPropertySet::SlicerPropertySet(Pala::Slicer* slicer)
42  : p(new Pala::SlicerPropertySet::Private)
43 {
44  p->m_slicer = slicer;
45 }
46 
47 Pala::SlicerPropertySet::~SlicerPropertySet()
48 {
49  delete p;
50 }
51 
52 Pala::Slicer* Pala::SlicerPropertySet::slicer() const
53 {
54  return p->m_slicer;
55 }
56 
57 void Pala::SlicerPropertySet::addPropertyToSlicer(const QByteArray& key, Pala::SlicerProperty* property)
58 {
59  p->m_slicer->addProperty(key, property);
60 }
61 
62 //END Pala::SlicerPropertySet
63 
64 //BEGIN Pala::SimpleGridPropertySet
65 
66 Pala::SimpleGridPropertySet::SimpleGridPropertySet(Pala::Slicer* slicer)
67  : Pala::SlicerPropertySet(slicer)
68  , p(0)
69 {
70  Pala::IntegerProperty* prop;
71  prop = new Pala::IntegerProperty(i18n("Piece count"));
72  prop->setRange(4, 10000);
73  prop->setDefaultValue(100);
74  addPropertyToSlicer("PieceCount", prop);
75  prop = new Pala::IntegerProperty(i18n("Piece aspect ratio"));
76  prop->setRange(0, 10);
77  prop->setDefaultValue(5);
78  prop->setRepresentation(Pala::IntegerProperty::Slider);
79  addPropertyToSlicer("PieceAspect", prop);
80 }
81 
82 Pala::SimpleGridPropertySet::~SimpleGridPropertySet()
83 {
84  delete p;
85 }
86 
87 QSize Pala::SimpleGridPropertySet::pieceCount(Pala::SlicerJob* job) const
88 {
89  const qreal imageAspect = qreal(job->image().width()) / qreal(job->image().height());
90  const qreal pieceAspect = pow(2.0, qreal(job->argument("PieceAspect").toInt() - 5) * 0.2);
91  const int count = job->argument("PieceCount").toInt();
92 
93  QSize bestCount(10, 10);
94  qreal bestQ = 1.0e100;
95 
96  qreal aspect = imageAspect / pieceAspect;
97  int maxX = ceil(sqrt(qreal(count) * aspect)) + 5;
98  int maxY = ceil(sqrt(qreal(count) / aspect)) + 5;
99  qDebug() << "Determining counts for total count" << count;
100  qDebug() << " Piece aspect ratio is" << pieceAspect;
101  qDebug() << " Image aspect is" << imageAspect;
102  qDebug() << " Target count aspect is" << aspect;
103  qDebug() << " Will try x <" << maxX << ", y <" << maxY;
104 
105  for (int x = 1; x < maxX; ++x)
106  {
107  for (int y = 1; y < maxY; ++y)
108  {
109  const int c = x * y;
110  const qreal xa = x / aspect;
111  const qreal a = pow((xa > y ? xa / qreal(y) : qreal(y) / xa) - 1.0, 2.0) * 20.0;
112  const qreal dev = pow(6.0 * (count - c) / count, 2.0);
113  const qreal q = (a + 1.0) * (dev + 1.0);
114 
115  if (q < bestQ)
116  {
117  bestQ = q;
118  bestCount = QSize(x, y);
119  }
120  }
121  }
122  qDebug() << "We liked " << bestCount << " ( at quality" << bestQ << ")";
123  return bestCount;
124 }
125 
126 //END Pala::SimpleGridPropertySet
Pala::SlicerProperty::setDefaultValue
void setDefaultValue(const QVariant &value)
Sets the default value of this property.
Definition: slicerproperty.cpp:113
slicer.h
slicerpropertyset.h
QByteArray
Pala::SimpleGridPropertySet::pieceCount
QSize pieceCount(Pala::SlicerJob *job) const
Definition: slicerpropertyset.cpp:87
Pala::IntegerProperty::setRange
void setRange(int min, int max)
Limits the user input to the selection of a number inside the given range (including the bounds)...
Definition: slicerproperty.cpp:167
Pala::SlicerJob::argument
QVariant argument(const QByteArray &key) const
Returns an argument of this job, i.e. the value that the user has chosen for the slicing algorithm's ...
Definition: slicerjob.cpp:54
Pala::SlicerJob::image
QImage image() const
Returns the image that should be sliced.
Definition: slicerjob.cpp:59
Pala::SlicerJob
Representation of a single run of a slicing algorithm.
Definition: slicerjob.h:46
Pala::SlicerPropertySet
Representation of a set of configurable parameters of a slicing algorithm.
Definition: slicerpropertyset.h:49
QVariant::toInt
int toInt(bool *ok) const
QImage::width
int width() const
Pala::IntegerProperty
Definition: slicerproperty.h:106
Pala::SlicerPropertySet::slicer
Pala::Slicer * slicer() const
Definition: slicerpropertyset.cpp:52
Pala::SlicerPropertySet::addPropertyToSlicer
void addPropertyToSlicer(const QByteArray &key, Pala::SlicerProperty *property)
A synonym for Pala::Slicer::addProperty (because the latter is "protected"; this abstract base class ...
Definition: slicerpropertyset.cpp:57
Pala::SlicerPropertySet::SlicerPropertySet
SlicerPropertySet(Pala::Slicer *slicer)
Definition: slicerpropertyset.cpp:41
QSize
Pala::IntegerProperty::Slider
Definition: slicerproperty.h:110
Pala::Slicer
Representation of a slicing algorithm.
Definition: slicer.h:58
Pala::SlicerPropertySet::~SlicerPropertySet
~SlicerPropertySet()
Definition: slicerpropertyset.cpp:47
slicerproperty.h
Pala::IntegerProperty::setRepresentation
void setRepresentation(Representation representation)
Decides how the property is represented in the user interface of Palapeli.
Definition: slicerproperty.cpp:173
slicerjob.h
Pala::SimpleGridPropertySet::SimpleGridPropertySet
SimpleGridPropertySet(Pala::Slicer *slicer)
Definition: slicerpropertyset.cpp:66
Pala::SimpleGridPropertySet::~SimpleGridPropertySet
~SimpleGridPropertySet()
Definition: slicerpropertyset.cpp:82
QImage::height
int height() const
Pala::SlicerProperty
Representation of a single configurable parameter of a slicing algorithm.
Definition: slicerproperty.h:44
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:19:02 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

palapeli/libpala

Skip menu "palapeli/libpala"
  • Main Page
  • Namespace List
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdegames API Reference

Skip menu "kdegames API Reference"
  • granatier
  • kapman
  • kblackbox
  • kgoldrunner
  • kigo
  • kmahjongg
  • KShisen
  • ksquares
  • libkdegames
  •   highscore
  •   libkdegamesprivate
  •     kgame
  • libkmahjongg
  • palapeli
  •   libpala

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal