Fixes: ensure same order of units in whole Calligra UI, have ruler and page...

Fixes: ensure same order of units in whole Calligra UI, have ruler and page layout dialog update to change of document's unit type

also tune KoUnit API a little:
* rename KoUnit::unit(...) to KoUnit::fromSymbol(...)   <- more Qt'ish
* turn static KoUnit::unitName(KoUnit) into member method KoUnit::symbol()   <- as typical use-case is on existing KoUnit instance, also shorter and more OOed
* rename KoUnit::Unit to KoUnit::Type    <- "type" feels a better term here
* added KoUnit::type() and KoUnit::setFactor(...)    <- useful in a few places
* remove KoUnit::unitDescription(...) from API    <- not used outside
* rename KoUnit::PixelVisibility to KoUnit:: ListOption  <- more general, some might want to add other flags like HideImperial

And adds unit tests for the touched KoUnit API

Thanks to boemann and Cyrille for making it compile with Qt 4.7 as well

REVIEW: 104626
parent 8e079386
......@@ -62,8 +62,8 @@ DlgImageSize::DlgImageSize(QWidget *parent, int width, int height, double resolu
m_page->cmbFilterType->setIDList(KisFilterStrategyRegistry::instance()->listKeys());
m_page->cmbFilterType->setCurrent("Bicubic");
m_page->cmbWidthUnit->addItems(KoUnit::listOfUnitName());
m_page->cmbHeightUnit->addItems(KoUnit::listOfUnitName());
m_page->cmbWidthUnit->addItems(KoUnit::listOfUnitNameForUi(KoUnit::HidePixel));
m_page->cmbHeightUnit->addItems(KoUnit::listOfUnitNameForUi(KoUnit::HidePixel));
m_page->doubleResolution->setValue(72.0 * resolution);
......@@ -140,8 +140,9 @@ DlgImageSize::DlgImageSize(QWidget *parent, int width, int height, double resolu
#warning "DlgImageSize: should take current units from a setting"
#endif
m_page->cmbWidthUnit->setCurrentIndex(KoUnit::Centimeter);
m_page->cmbHeightUnit->setCurrentIndex(KoUnit::Centimeter);
const int unitIndex = KoUnit(KoUnit::Centimeter).indexInListForUi(KoUnit::HidePixel);
m_page->cmbWidthUnit->setCurrentIndex(unitIndex);
m_page->cmbHeightUnit->setCurrentIndex(unitIndex);
connect(this, SIGNAL(okClicked()),
this, SLOT(okClicked()));
......@@ -188,7 +189,7 @@ void DlgImageSize::slotWidthPixelsChanged(int w)
if (m_page->lock_resolution->isLocked()) { // !m_page->chkAffectResolution->isChecked()) {
m_width = 72 * w / m_page->doubleResolution->value();
KoUnit unit = KoUnit((KoUnit::Unit)m_page->cmbWidthUnit->currentIndex());
const KoUnit unit = KoUnit::fromListForUi(m_page->cmbWidthUnit->currentIndex(), KoUnit::HidePixel);
m_page->doublePhysicalWidth->setValue(unit.toUserValue(m_width));
} else {
m_page->doubleResolution->setValue(72 * w / m_width);
......@@ -199,7 +200,7 @@ void DlgImageSize::slotWidthPixelsChanged(int w)
if (m_page->aspectPixels->keepAspectRatio()) {
m_height = m_width / m_aspectRatio;
KoUnit unit = KoUnit((KoUnit::Unit)m_page->cmbHeightUnit->currentIndex());
const KoUnit unit = KoUnit::fromListForUi(m_page->cmbHeightUnit->currentIndex(), KoUnit::HidePixel);
m_page->doublePhysicalHeight->setValue(unit.toUserValue(m_height));
m_page->intPixelHeight->setValue(int(0.5 + m_height * m_page->doubleResolution->value() / 72.0));
......@@ -222,7 +223,7 @@ void DlgImageSize::slotHeightPixelsChanged(int h)
if (m_page->lock_resolution->isLocked()) { // !m_page->chkAffectResolution->isChecked()) {
m_height = 72 * h / m_page->doubleResolution->value();
KoUnit unit = KoUnit((KoUnit::Unit)m_page->cmbHeightUnit->currentIndex());
const KoUnit unit = KoUnit::fromListForUi(m_page->cmbHeightUnit->currentIndex(), KoUnit::HidePixel);
m_page->doublePhysicalHeight->setValue(unit.toUserValue(m_height));
} else {
m_page->doubleResolution->setValue(72 * h / m_height);
......@@ -233,7 +234,7 @@ void DlgImageSize::slotHeightPixelsChanged(int h)
if (m_page->aspectPixels->keepAspectRatio()) {
m_width = m_aspectRatio * m_height;
KoUnit unit = KoUnit((KoUnit::Unit)m_page->cmbWidthUnit->currentIndex());
const KoUnit unit = KoUnit::fromListForUi(m_page->cmbWidthPixelUnit->currentIndex(), KoUnit::HidePixel);
m_page->doublePhysicalWidth->setValue(unit.toUserValue(m_width));
m_page->intPixelWidth->setValue(int(0.5 + m_width * m_page->doubleResolution->value() / 72.0));
......@@ -287,7 +288,7 @@ void DlgImageSize::slotWidthPhysicalChanged(double w)
{
blockAll();
KoUnit unit = KoUnit((KoUnit::Unit)m_page->cmbWidthUnit->currentIndex());
KoUnit unit = KoUnit::fromListForUi(m_page->cmbWidthPixelUnit->currentIndex(), KoUnit::HidePixel);
m_width = unit.fromUserValue(w);
if (m_page->lock_resolution->isLocked()) { // !m_page->chkAffectResolution->isChecked()) {
......@@ -298,14 +299,14 @@ void DlgImageSize::slotWidthPhysicalChanged(double w)
// since we only have one resolution parameter we need to recalculate the physical height
m_height = 72 * m_page->intPixelHeight->value() / m_page->doubleResolution->value();
unit = KoUnit((KoUnit::Unit)m_page->cmbHeightUnit->currentIndex());
unit = KoUnit::fromListForUi(m_page->cmbHeightUnit->currentIndex(), KoUnit::HidePixel);
m_page->doublePhysicalHeight->setValue(unit.toUserValue(m_height));
}
if (m_page->aspectPixels->keepAspectRatio()) {
m_height = m_width / m_aspectRatio;
unit = KoUnit((KoUnit::Unit)m_page->cmbHeightUnit->currentIndex());
unit = KoUnit::fromListForUi(m_page->cmbHeightUnit->currentIndex(), KoUnit::HidePixel);
m_page->doublePhysicalHeight->setValue(unit.toUserValue(m_height));
m_page->intPixelHeight->setValue(int(0.5 + m_height * m_page->doubleResolution->value() / 72.0));
......@@ -322,7 +323,7 @@ void DlgImageSize::slotHeightPhysicalChanged(double h)
{
blockAll();
KoUnit unit = KoUnit((KoUnit::Unit)m_page->cmbHeightUnit->currentIndex());
KoUnit unit = KoUnit::fromListForUi(m_page->cmbHeightUnit->currentIndex(), KoUnit::HidePixel);
m_height = unit.fromUserValue(h);
if (m_page->lock_resolution->isLocked()) { // !m_page->chkAffectResolution->isChecked()) {
......@@ -334,14 +335,14 @@ void DlgImageSize::slotHeightPhysicalChanged(double h)
// since we only have one resolution parameter we need to recalculate the physical width
m_width = 72 * m_page->intPixelWidth->value() / m_page->doubleResolution->value();
unit = KoUnit((KoUnit::Unit)m_page->cmbWidthUnit->currentIndex());
unit = KoUnit::fromListForUi(m_page->cmbWidthUnit->currentIndex(), KoUnit::HidePixel);
m_page->doublePhysicalWidth->setValue(unit.toUserValue(m_width));
}
if (m_page->aspectPixels->keepAspectRatio()) {
m_width = m_aspectRatio * m_height;
unit = KoUnit((KoUnit::Unit)m_page->cmbWidthUnit->currentIndex());
unit = KoUnit::fromListForUi(m_page->cmbWidthUnit->currentIndex(), KoUnit::HidePixel);
m_page->doublePhysicalWidth->setValue(unit.toUserValue(m_width));
m_page->intPixelWidth->setValue(int(0.5 + m_width * m_page->doubleResolution->value() / 72.0));
......@@ -358,10 +359,9 @@ void DlgImageSize::slotWidthUnitChanged(int index)
{
blockAll();
KoUnit unit = KoUnit((KoUnit::Unit)index);
const KoUnit unit = KoUnit::fromListForUi(index, KoUnit::HidePixel);
m_page->doublePhysicalWidth->setValue(unit.toUserValue(m_width));
unblockAll();
}
......@@ -369,7 +369,7 @@ void DlgImageSize::slotHeightUnitChanged(int index)
{
blockAll();
KoUnit unit = KoUnit((KoUnit::Unit)index);
const KoUnit unit = KoUnit::fromListForUi(index, KoUnit::HidePixel);
m_page->doublePhysicalHeight->setValue(unit.toUserValue(m_height));
unblockAll();
......@@ -389,7 +389,7 @@ void DlgImageSize::slotResolutionChanged(double r)
m_width = 72 * m_page->intPixelWidth->value() / m_page->doubleResolution->value();
m_height = 72 * m_page->intPixelHeight->value() / m_page->doubleResolution->value();
KoUnit unit = KoUnit((KoUnit::Unit)m_page->cmbWidthUnit->currentIndex());
const KoUnit unit = KoUnit::fromListForUi(m_page->cmbWidthUnit->currentIndex(), KoUnit::HidePixel);
m_page->doublePhysicalWidth->setValue(unit.toUserValue(m_width));
m_page->doublePhysicalHeight->setValue(unit.toUserValue(m_height));
}
......
......@@ -39,7 +39,8 @@
KisToolMeasureOptionsWidget::KisToolMeasureOptionsWidget(QWidget* parent, double resolution)
: QWidget(parent),
m_resolution(resolution)
m_resolution(resolution),
m_unit(KoUnit::Pixel)
{
m_distance = 0.0;
......@@ -60,9 +61,9 @@ KisToolMeasureOptionsWidget::KisToolMeasureOptionsWidget(QWidget* parent, double
optionLayout->addWidget(m_angleLabel, 1, 1);
KComboBox* unitBox = new KComboBox(this);
unitBox->addItems(KoUnit::listOfUnitName(false));
unitBox->addItems(KoUnit::listOfUnitNameForUi(KoUnit::ListAll));
connect(unitBox, SIGNAL(currentIndexChanged(int)), this, SLOT(slotUnitChanged(int)));
unitBox->setCurrentIndex(KoUnit::Pixel);
unitBox->setCurrentIndex(m_unit.indexInListForUi(KoUnit::ListAll));
optionLayout->addWidget(unitBox, 0, 2);
optionLayout->addWidget(new QLabel("deg", this), 1, 2);
......@@ -82,7 +83,7 @@ void KisToolMeasureOptionsWidget::slotSetAngle(double angle)
void KisToolMeasureOptionsWidget::slotUnitChanged(int index)
{
m_unit = KoUnit((KoUnit::Unit)index, m_resolution);
m_unit = KoUnit::fromListForUi(index, KoUnit::ListAll, m_resolution);
updateDistance();
}
......
......@@ -74,15 +74,15 @@ KisCustomImageWidget::KisCustomImageWidget(QWidget* parent, KisDoc2* doc, qint32
doubleWidth->setValue(defWidth);
doubleWidth->setDecimals(0);
m_width = m_widthUnit.fromUserValue(defWidth);
cmbWidthUnit->addItems(KoUnit::listOfUnitName(false));
cmbWidthUnit->setCurrentIndex(KoUnit::Pixel);
cmbWidthUnit->addItems(KoUnit::listOfUnitNameForUi(KoUnit::ListAll));
cmbWidthUnit->setCurrentIndex(m_widthUnit.indexInListForUi(KoUnit::ListAll));
m_heightUnit = KoUnit(KoUnit::Pixel, resolution);
doubleHeight->setValue(defHeight);
doubleHeight->setDecimals(0);
m_height = m_heightUnit.fromUserValue(defHeight);
cmbHeightUnit->addItems(KoUnit::listOfUnitName(false));
cmbHeightUnit->setCurrentIndex(KoUnit::Pixel);
cmbHeightUnit->addItems(KoUnit::listOfUnitNameForUi(KoUnit::ListAll));
cmbHeightUnit->setCurrentIndex(m_heightUnit.indexInListForUi(KoUnit::ListAll));
doubleResolution->setValue(72.0 * resolution);
doubleResolution->setDecimals(0);
......@@ -132,13 +132,13 @@ KisCustomImageWidget::~KisCustomImageWidget()
void KisCustomImageWidget::resolutionChanged(double res)
{
if (m_widthUnit.indexInList(KoUnit::ShowAll) == KoUnit::Pixel) {
m_widthUnit = KoUnit(KoUnit::Pixel, res / 72.0);
if (m_widthUnit.type() == KoUnit::Pixel) {
m_widthUnit.setFactor(res / 72.0);
m_width = m_widthUnit.fromUserValue(doubleWidth->value());
}
if (m_heightUnit.indexInList(KoUnit::ShowAll) == KoUnit::Pixel) {
m_heightUnit = KoUnit(KoUnit::Pixel, res / 72.0);
if (m_heightUnit.type() == KoUnit::Pixel) {
m_heightUnit.setFactor(res / 72.0);
m_height = m_heightUnit.fromUserValue(doubleHeight->value());
}
}
......@@ -148,12 +148,12 @@ void KisCustomImageWidget::widthUnitChanged(int index)
{
doubleWidth->blockSignals(true);
if (index == KoUnit::Pixel) {
m_widthUnit = KoUnit::fromListForUi(index, KoUnit::ListAll);
if (m_widthUnit.type() == KoUnit::Pixel) {
doubleWidth->setDecimals(0);
m_widthUnit = KoUnit(KoUnit::Pixel, doubleResolution->value() / 72.0);
m_widthUnit.setFactor(doubleResolution->value() / 72.0);
} else {
doubleWidth->setDecimals(2);
m_widthUnit = KoUnit((KoUnit::Unit)cmbWidthUnit->currentIndex());
}
doubleWidth->setValue(KoUnit::ptToUnit(m_width, m_widthUnit));
......@@ -170,12 +170,12 @@ void KisCustomImageWidget::heightUnitChanged(int index)
{
doubleHeight->blockSignals(true);
if (index == KoUnit::Pixel) {
m_widthUnit = KoUnit::fromListForUi(index, KoUnit::ListAll);
if (m_widthUnit.type() == KoUnit::Pixel) {
doubleHeight->setDecimals(0);
m_heightUnit = KoUnit(KoUnit::Pixel, doubleResolution->value() / 72.0);
m_widthUnit.setFactor(doubleResolution->value() / 72.0);
} else {
doubleHeight->setDecimals(2);
m_heightUnit = KoUnit((KoUnit::Unit)cmbHeightUnit->currentIndex());
}
doubleHeight->setValue(KoUnit::ptToUnit(m_height, m_heightUnit));
......@@ -273,8 +273,10 @@ void KisCustomImageWidget::clipboardDataChanged()
void KisCustomImageWidget::screenSizeClicked()
{
QSize sz = QApplication::desktop()->screenGeometry(this).size();
cmbWidthUnit->setCurrentIndex(KoUnit::Pixel);
cmbHeightUnit->setCurrentIndex(KoUnit::Pixel);
const int index = KoUnit(KoUnit::Pixel).indexInListForUi(KoUnit::ListAll);
cmbWidthUnit->setCurrentIndex(index);
cmbHeightUnit->setCurrentIndex(index);
widthUnitChanged(cmbWidthUnit->currentIndex());
heightUnitChanged(cmbHeightUnit->currentIndex());
......
......@@ -440,7 +440,7 @@ void KoPADocument::loadOdfSettings( const KoXmlDocument & settingsDoc )
KoOasisSettings settings( settingsDoc );
KoOasisSettings::Items viewSettings = settings.itemSet( "view-settings" );
if ( !viewSettings.isNull() ) {
setUnit( KoUnit::unit( viewSettings.parseConfigItemString( "unit" ) ) );
setUnit(KoUnit::fromSymbol(viewSettings.parseConfigItemString("unit")));
// FIXME: add other config here.
}
......
......@@ -47,7 +47,7 @@ KoPAConfigureDialog::KoPAConfigureDialog(KoPAView* parent)
item->setHeader(i18n("Grid"));
item->setIcon(KIcon(BarIcon("grid", KIconLoader::SizeMedium)));
connect(m_miscPage, SIGNAL(unitChanged(int)), m_gridPage, SLOT(slotUnitChanged(int)));
connect(m_miscPage, SIGNAL(unitChanged(KoUnit)), m_gridPage, SLOT(slotUnitChanged(KoUnit)));
m_docPage = new KoConfigDocumentPage( parent->koDocument() );
item = addPage( m_docPage, i18nc( "@title:tab Document settings page", "Document" ) );
......
......@@ -319,7 +319,7 @@ void DoubleSpinBox::setValue(double v)
{
#ifdef KOPROPERTY_USE_KOLIBS
if (!m_unit.isEmpty()) {
KDoubleNumInput::setValue(KoUnit::unit(m_unit).toUserValue(v));
KDoubleNumInput::setValue(KoUnit::fromSymbol(m_unit).toUserValue(v));
return;
}
#endif
......@@ -330,7 +330,7 @@ double DoubleSpinBox::value() const
{
#ifdef KOPROPERTY_USE_KOLIBS
if (!m_unit.isEmpty()) {
return KoUnit::unit(m_unit).fromUserValue(KDoubleNumInput::value());
return KoUnit::fromSymbol(m_unit).fromUserValue(KDoubleNumInput::value());
}
#endif
return KDoubleNumInput::value();
......@@ -545,7 +545,8 @@ QString DoubleSpinBoxDelegate::displayTextForProperty( const Property* prop ) co
QString display;
#ifdef KOPROPERTY_USE_KOLIBS
if (!unit.isEmpty()) {
return KGlobal::locale()->formatNumber(KoUnit::unit(unit).toUserValue(prop->value().toDouble())) + ' ' + unit;
return KGlobal::locale()->formatNumber(KoUnit::fromSymbol(unit).toUserValue(prop->value().toDouble())) +
QLatin1Char(' ') + unit;
}
#endif
return KGlobal::locale()->formatNumber(prop->value().toDouble());
......
......@@ -84,7 +84,7 @@ bool KoReportItemBase::parseReportLineStyleData(const QDomElement & elemSource,
bool KoReportItemBase::parseReportRect(const QDomElement & elemSource, KRPos *pos, KRSize *siz)
{
QString sUnit = elemSource.attribute("svg:x", "1cm").right(2);
KoUnit unit = KoUnit::unit(sUnit);
const KoUnit unit = KoUnit::fromSymbol(sUnit);
pos->setUnit(unit);
siz->setUnit(unit);
QPointF _pos;
......
......@@ -106,7 +106,7 @@ void KRSectionData::createProperties(const QDomElement & elemSource)
{
m_set = new KoProperty::Set(this, "Section");
m_height = new KoProperty::Property("height", KoUnit::unit("cm").fromUserValue(2.0), i18n("Height"));
m_height = new KoProperty::Property("height", KoUnit(KoUnit::Centimeter).fromUserValue(2.0), i18n("Height"));
m_backgroundColor = new KoProperty::Property("background-color", Qt::white, i18n("Background Color"));
m_height->setOption("unit", "cm");
if (!elemSource.isNull())
......
......@@ -329,10 +329,10 @@ QDomElement KoReportDesigner::document() const
KoReportDesignerItemBase::addPropertyAsAttribute(&pagestyle, m_orientation);
// -- margins
pagestyle.setAttribute("fo:margin-top", KoUnit::unit(m_topMargin->option("unit").toString()).toUserStringValue(m_topMargin->value().toDouble()) + m_topMargin->option("unit").toString());
pagestyle.setAttribute("fo:margin-bottom", KoUnit::unit(m_bottomMargin->option("unit").toString()).toUserStringValue(m_bottomMargin->value().toDouble()) + m_topMargin->option("unit").toString());
pagestyle.setAttribute("fo:margin-right", KoUnit::unit(m_rightMargin->option("unit").toString()).toUserStringValue(m_rightMargin->value().toDouble()) + m_topMargin->option("unit").toString());
pagestyle.setAttribute("fo:margin-left", KoUnit::unit(m_leftMargin->option("unit").toString()).toUserStringValue(m_leftMargin->value().toDouble()) + m_topMargin->option("unit").toString());
pagestyle.setAttribute("fo:margin-top", KoUnit::fromSymbol(m_topMargin->option("unit").toString()).toUserStringValue(m_topMargin->value().toDouble()) + m_topMargin->option("unit").toString());
pagestyle.setAttribute("fo:margin-bottom", KoUnit::fromSymbol(m_bottomMargin->option("unit").toString()).toUserStringValue(m_bottomMargin->value().toDouble()) + m_topMargin->option("unit").toString());
pagestyle.setAttribute("fo:margin-right", KoUnit::fromSymbol(m_rightMargin->option("unit").toString()).toUserStringValue(m_rightMargin->value().toDouble()) + m_topMargin->option("unit").toString());
pagestyle.setAttribute("fo:margin-left", KoUnit::fromSymbol(m_leftMargin->option("unit").toString()).toUserStringValue(m_leftMargin->value().toDouble()) + m_topMargin->option("unit").toString());
content.appendChild(pagestyle);
......@@ -619,7 +619,7 @@ void KoReportDesigner::createProperties()
keys.clear(); strings.clear();
strings = KoUnit::listOfUnitName();
strings = KoUnit::listOfUnitNameForUi(KoUnit::HidePixel);
QString unit;
foreach(const QString &un, strings) {
unit = un.mid(un.indexOf('(') + 1, 2);
......@@ -632,13 +632,13 @@ void KoReportDesigner::createProperties()
m_gridSnap = new KoProperty::Property("grid-snap", true, i18n("Grid Snap"), i18n("Grid Snap"));
m_gridDivisions = new KoProperty::Property("grid-divisions", 4, i18n("Grid Divisions"), i18n("Grid Divisions"));
m_leftMargin = new KoProperty::Property("margin-left", KoUnit::unit("cm").fromUserValue(1.0),
m_leftMargin = new KoProperty::Property("margin-left", KoUnit(KoUnit::Centimeter).fromUserValue(1.0),
i18n("Left Margin"), i18n("Left Margin"), KoProperty::Double);
m_rightMargin = new KoProperty::Property("margin-right", KoUnit::unit("cm").fromUserValue(1.0),
m_rightMargin = new KoProperty::Property("margin-right", KoUnit(KoUnit::Centimeter).fromUserValue(1.0),
i18n("Right Margin"), i18n("Right Margin"), KoProperty::Double);
m_topMargin = new KoProperty::Property("margin-top", KoUnit::unit("cm").fromUserValue(1.0),
m_topMargin = new KoProperty::Property("margin-top", KoUnit(KoUnit::Centimeter).fromUserValue(1.0),
i18n("Top Margin"), i18n("Top Margin"), KoProperty::Double);
m_bottomMargin = new KoProperty::Property("margin-bottom", KoUnit::unit("cm").fromUserValue(1.0),
m_bottomMargin = new KoProperty::Property("margin-bottom", KoUnit(KoUnit::Centimeter).fromUserValue(1.0),
i18n("Bottom Margin"), i18n("Bottom Margin"), KoProperty::Double);
m_leftMargin->setOption("unit", "cm");
m_rightMargin->setOption("unit", "cm");
......@@ -800,9 +800,9 @@ KoUnit KoReportDesigner::pageUnit() const
u = m_unit->value().toString();
KoUnit unit = KoUnit::unit(u, &found);
KoUnit unit = KoUnit::fromSymbol(u, &found);
if (!found) {
unit = KoUnit::unit("cm");
unit = KoUnit(KoUnit::Centimeter);
}
return unit;
......
......@@ -63,12 +63,12 @@ void KoReportDesignerItemBase::buildXML(QGraphicsItem * item, QDomDocument & doc
void KoReportDesignerItemBase::buildXMLRect(QDomDocument & doc, QDomElement & entity, KRPos *pos, KRSize *siz)
{
Q_UNUSED(doc)
KoUnit unit = pos->unit();
entity.setAttribute("svg:x", QString::number(pos->toUnit().x()) + KoUnit::unitName(unit));
entity.setAttribute("svg:y", QString::number(pos->toUnit().y()) + KoUnit::unitName(unit));
entity.setAttribute("svg:width", QString::number(siz->toUnit().width()) + KoUnit::unitName(unit));
entity.setAttribute("svg:height", QString::number(siz->toUnit().height()) + KoUnit::unitName(unit));
const QString unitSymbol = pos->unit().symbol();
entity.setAttribute("svg:x", QString::number(pos->toUnit().x()) + unitSymbol);
entity.setAttribute("svg:y", QString::number(pos->toUnit().y()) + unitSymbol);
entity.setAttribute("svg:width", QString::number(siz->toUnit().width()) + unitSymbol);
entity.setAttribute("svg:height", QString::number(siz->toUnit().height()) + unitSymbol);
}
void KoReportDesignerItemBase::buildXMLTextStyle(QDomDocument & doc, QDomElement & entity, KRTextStyleData ts)
......
......@@ -113,7 +113,7 @@ void KoReportDesignerItemLine::buildXML(QDomDocument & doc, QDomElement & parent
QDomElement entity = doc.createElement("report:line");
qreal sx, sy, ex, ey;
QString unitname = KoUnit::unitName(m_start.unit());
QString unitname = m_start.unit().symbol();
sx = m_start.toUnit().x();
sy = m_start.toUnit().y();
......
......@@ -41,12 +41,14 @@ ReportScene::ReportScene(qreal w, qreal h, KoReportDesigner *rd)
{
m_rd = rd;
if (KoUnit::unitName(m_unit) != KoUnit::unitName(m_rd->pageUnit())) {
if (m_unit.type() != m_rd->pageUnit().type()) {
m_unit = m_rd->pageUnit();
if (KoUnit::unitName(m_unit) == "cc" || KoUnit::unitName(m_unit) == "pi" || KoUnit::unitName(m_unit) == "mm") {
if (m_unit.type() == KoUnit::Cicero ||
m_unit.type() == KoUnit::Pica ||
m_unit.type() == KoUnit::Millimeter) {
m_majorX = POINT_TO_INCH(m_unit.fromUserValue(10)) * KoDpi::dpiX();
m_majorY = POINT_TO_INCH(m_unit.fromUserValue(10)) * KoDpi::dpiY();
} else if (KoUnit::unitName(m_unit) == "pt") {
} else if (m_unit.type() == KoUnit::Point) {
m_majorX = POINT_TO_INCH(m_unit.fromUserValue(100)) * KoDpi::dpiX();
m_majorY = POINT_TO_INCH(m_unit.fromUserValue(100)) * KoDpi::dpiY();
} else {
......@@ -67,12 +69,14 @@ void ReportScene::drawBackground(QPainter* painter, const QRectF & clip)
painter->setRenderHint(QPainter::Antialiasing, false);
if (m_rd->propertySet()->property("grid-visible").value().toBool()) {
if (KoUnit::unitName(m_unit) != KoUnit::unitName(m_rd->pageUnit())) {
if (m_unit.type() != m_rd->pageUnit().type()) {
m_unit = m_rd->pageUnit();
if (KoUnit::unitName(m_unit) == "cc" || KoUnit::unitName(m_unit) == "pi" || KoUnit::unitName(m_unit) == "mm") {
if (m_unit.type() == KoUnit::Cicero ||
m_unit.type() == KoUnit::Pica ||
m_unit.type() == KoUnit::Millimeter) {
m_majorX = POINT_TO_INCH(m_unit.fromUserValue(10)) * KoDpi::dpiX();
m_majorY = POINT_TO_INCH(m_unit.fromUserValue(10)) * KoDpi::dpiY();
} else if (KoUnit::unitName(m_unit) == "pt") {
} else if (m_unit.type() == KoUnit::Point) {
m_majorX = POINT_TO_INCH(m_unit.fromUserValue(100)) * KoDpi::dpiX();
m_majorY = POINT_TO_INCH(m_unit.fromUserValue(100)) * KoDpi::dpiY();
} else {
......@@ -146,14 +150,17 @@ QPointF ReportScene::gridPoint(const QPointF& p)
return p;
}
if (KoUnit::unitName(m_unit) != KoUnit::unitName(m_rd->pageUnit())) {
if (m_unit.type() != m_rd->pageUnit().type()) {
m_unit = m_rd->pageUnit();
if (KoUnit::unitName(m_unit) != KoUnit::unitName(m_rd->pageUnit())) {
// TODO: Again? Copy&Paste error?
if (m_unit.type() != m_rd->pageUnit().type()) {
m_unit = m_rd->pageUnit();
if (KoUnit::unitName(m_unit) == "cc" || KoUnit::unitName(m_unit) == "pi" || KoUnit::unitName(m_unit) == "mm") {
if (m_unit.type() == KoUnit::Cicero ||
m_unit.type() == KoUnit::Pica ||
m_unit.type() == KoUnit::Millimeter) {
m_majorX = POINT_TO_INCH(m_unit.fromUserValue(10)) * KoDpi::dpiX();
m_majorY = POINT_TO_INCH(m_unit.fromUserValue(10)) * KoDpi::dpiY();
} else if (KoUnit::unitName(m_unit) == "pt") {
} else if (m_unit.type() == KoUnit::Point) {
m_majorX = POINT_TO_INCH(m_unit.fromUserValue(100)) * KoDpi::dpiX();
m_majorY = POINT_TO_INCH(m_unit.fromUserValue(100)) * KoDpi::dpiY();
} else {
......
......@@ -151,7 +151,7 @@ void ReportSection::buildXML(QDomDocument &doc, QDomElement &section)
{
QString un = m_sectionData->m_height->option("unit", "cm").toString();
section.setAttribute("svg:height", KoUnit::unit(un).toUserStringValue(m_sectionData->m_height->value().toDouble()) + un);
section.setAttribute("svg:height", KoUnit::fromSymbol(un).toUserStringValue(m_sectionData->m_height->value().toDouble()) + un);
section.setAttribute("fo:background-color", m_sectionData->backgroundColor().name());
// now get a list of all the QGraphicsItems on this scene and output them.
......@@ -217,7 +217,7 @@ void ReportSection::slotPageOptionsChanged(KoProperty::Set &set)
KoUnit unit = m_reportDesigner->pageUnit();
m_sectionData->m_height->setOption("unit", KoUnit::unitName(unit));
m_sectionData->m_height->setOption("unit", unit.symbol());
//update items position with unit
QList<QGraphicsItem*> itms = m_scene->items();
......
......@@ -2605,12 +2605,7 @@ void KoDocument::setUnit(const KoUnit &unit)
void KoDocument::saveUnitOdf(KoXmlWriter *settingsWriter) const
{
settingsWriter->addConfigItem("unit", unitName());
}
QString KoDocument::unitName() const
{
return KoUnit::unitName(unit());
settingsWriter->addConfigItem("unit", unit().symbol());
}
void KoDocument::showStartUpWidget(KoMainWindow *mainWindow, bool alwaysShow)
......
......@@ -778,13 +778,6 @@ public:
*/
void saveUnitOdf(KoXmlWriter *settingsWriter) const;
/**
* Returns the name of the unit used to display all measures/distances.
* Use this method for displaying it in the user interface, but use
* unit() for everything else (conversions etc.)
*/
QString unitName() const;
/**
* Set the template type used. This is used by the start up widget to show
* the correct templates.
......
......@@ -253,7 +253,7 @@ void HorizontalPaintingStrategy::drawMeasurements(const KoRulerPrivate *d, QPain
qreal numberStep = d->numberStepForUnit(); // number step in unit
QRectF activeRangeRectangle;
int numberStepPixel = qRound(d->viewConverter->documentToViewX(d->unit.fromUserValue(numberStep)));
const bool adjustMillimeters = d->unit.indexInList() == KoUnit::Millimeter;
const bool adjustMillimeters = (d->unit.type() == KoUnit::Millimeter);
QFontMetrics fontMetrics(KGlobalSettings::toolBarFont());
if (numberStepPixel == 0 || numberStep == 0)
......@@ -485,7 +485,7 @@ void VerticalPaintingStrategy::drawMeasurements(const KoRulerPrivate *d, QPainte
QFontMetrics fontMetrics(KGlobalSettings::toolBarFont());
// Calc the longest text length
int textLength = 0;
const bool adjustMillimeters = d->unit.indexInList() == KoUnit::Millimeter;
const bool adjustMillimeters = (d->unit.type() == KoUnit::Millimeter);
for(int i = 0; i < lengthInPixel; i += numberStepPixel) {
int number = qRound((i / numberStepPixel) * numberStep);
if (adjustMillimeters)
......@@ -621,7 +621,7 @@ void HorizontalDistancesPaintingStrategy::drawDistanceLine(const KoRulerPrivate
font.setPointSize(6);
QFontMetrics fontMetrics(font);
QString label = d->unit.toUserStringValue(
d->viewConverter->viewToDocumentX(line.length())) + ' ' + KoUnit::unitName(d->unit);
d->viewConverter->viewToDocumentX(line.length())) + ' ' + d->unit.symbol();
QPointF labelPosition = QPointF(midPoint.x() - fontMetrics.width(label)/2,
midPoint.y() + fontMetrics.ascent()/2);
painter.setFont(font);
......@@ -711,7 +711,7 @@ KoRulerPrivate::~KoRulerPrivate()
qreal KoRulerPrivate::numberStepForUnit() const
{
switch(unit.indexInList()) {
switch(unit.type()) {
case KoUnit::Inch:
case KoUnit::Centimeter:
case KoUnit::Decimeter:
......
......@@ -562,21 +562,8 @@ QToolBar* KoView::viewBar()
QList<QAction*> KoView::createChangeUnitActions()
{
QActionGroup *unitGroup = new QActionGroup(this);
QList<QAction*> answer;
answer.append(new UnitChangeAction(KoUnit::Millimeter, unitGroup, d->document));
answer.append(new UnitChangeAction(KoUnit::Centimeter, unitGroup, d->document));
answer.append(new UnitChangeAction(KoUnit::Decimeter, unitGroup, d->document));
answer.append(new UnitChangeAction(KoUnit::Inch, unitGroup, d->document));
answer.append(new UnitChangeAction(KoUnit::Pica, unitGroup, d->document));
answer.append(new UnitChangeAction(KoUnit::Cicero, unitGroup, d->document));
answer.append(new UnitChangeAction(KoUnit::Point, unitGroup, d->document));
const int currentUnit = d->document.data()->unit().indexInList();
Q_ASSERT(currentUnit < answer.count());
if (currentUnit >= 0)
answer.value(currentUnit)->setChecked(true);
return answer;
UnitActionGroup* unitActions = new UnitActionGroup(d->document, this);
return unitActions->actions();
}
#include <KoView_p.moc>
......
/* This file is part of the KDE project
* Copyright (C) 2007 Thomas Zander <zander@kde.org>
/* This file is part of the Calligra project, made within the KDE community.
* Copyright 2012 Friedrich W. H. Kossebau <kossebau@kde.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
......@@ -20,31 +20,67 @@
#ifndef KOVIEW_P_H
#define KOVIEW_P_H
// Calligra
#include "KoUnit.h"
#include "KoDocument.h"
// Qt
#include <QActionGroup>
#include <QAction>
class UnitChangeAction : public QAction
// Action group which keeps the actions in sync with the document's unit property
class UnitActionGroup : public QActionGroup
{
Q_OBJECT
public:
UnitChangeAction(KoUnit::Unit unit, QObject *parent, KoDocument *document)
: QAction(KoUnit::unitDescription(KoUnit(unit)), parent),
m_document(document),
m_unit(unit) {
setCheckable(true);
connect(this, SIGNAL(triggered(bool)), SLOT(activated()));
explicit UnitActionGroup(KoDocument *document, QObject* parent = 0)
: QActionGroup(parent)
, m_document(document)
{
setExclusive(true);
connect(this, SIGNAL(triggered(QAction*)), SLOT(onTriggered(QAction*)));
connect(document, SIGNAL(unitChanged(KoUnit)), SLOT(onUnitChanged(KoUnit)));
const QStringList unitNames = KoUnit::listOfUnitNameForUi(KoUnit::HidePixel);
const int currentUnitIndex = m_document->unit().indexInListForUi(KoUnit::HidePixel);
for (int i = 0; i < unitNames.count(); ++i) {
QAction* action = new QAction(unitNames.at(i), this);
action->setData(i);
action->setCheckable(true);
if (currentUnitIndex == i) {
action->setChecked(true);
}
}
}
private slots:
void onTriggered(QAction *action)
{
m_document->setUnit(KoUnit::fromListForUi(action->data().toInt(), KoUnit::HidePixel));
}
public slots:
void activated() {
m_document->setUnit(m_unit);
void onUnitChanged(const KoUnit &unit)
{
const int indexInList = unit.indexInListForUi(KoUnit::HidePixel);
foreach (QAction *action, actions()) {
if (action->data().toInt() == indexInList) {
action->setChecked(true);
break;
}
// in case the new unit is not in the list of actions
// this ensures that the action currently checked is unchecked
// once the end of actions has been reached
if (action->isChecked()) {
action->setChecked(false);
}
}