File indexing completed on 2024-05-19 04:44:34
0001 /* This file is part of the KDE project 0002 * Copyright (C) 2007-2008 by Adam Pigg (adam@piggz.co.uk) 0003 * 0004 * This library is free software; you can redistribute it and/or 0005 * modify it under the terms of the GNU Lesser General Public 0006 * License as published by the Free Software Foundation; either 0007 * version 2.1 of the License, or (at your option) any later version. 0008 * 0009 * This library is distributed in the hope that it will be useful, 0010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0012 * Lesser General Public License for more details. 0013 * 0014 * You should have received a copy of the GNU Lesser General Public 0015 * License along with this library. If not, see <http://www.gnu.org/licenses/>. 0016 */ 0017 #include "KReportScriptText.h" 0018 0019 #include <QFile> 0020 #include <QTextStream> 0021 #include <QSizeF> 0022 #include <QPointF> 0023 0024 #include "kreportplugin_debug.h" 0025 0026 namespace Scripting 0027 { 0028 0029 Text::Text(KReportItemText* t) 0030 { 0031 m_text = t; 0032 } 0033 0034 0035 Text::~Text() 0036 { 0037 } 0038 0039 QString Text::source() const 0040 { 0041 return m_text->itemDataSource(); 0042 } 0043 0044 void Text::setSource(const QString& s) 0045 { 0046 m_text->setItemDataSource(s); 0047 } 0048 0049 int Text::horizontalAlignment() const 0050 { 0051 const QString a = m_text->m_horizontalAlignment->value().toString().toLower(); 0052 0053 if (a == QLatin1String("left")) { 0054 return -1; 0055 } 0056 if (a == QLatin1String("center")) { 0057 return 0; 0058 } 0059 if (a == QLatin1String("right")) { 0060 return 1; 0061 } 0062 return -1; 0063 } 0064 void Text::setHorizonalAlignment(int a) 0065 { 0066 switch (a) { 0067 case -1: 0068 m_text->m_horizontalAlignment->setValue(QLatin1String("left")); 0069 break; 0070 case 0: 0071 m_text->m_horizontalAlignment->setValue(QLatin1String("center")); 0072 break; 0073 case 1: 0074 m_text->m_horizontalAlignment->setValue(QLatin1String("right")); 0075 break; 0076 default: 0077 m_text->m_horizontalAlignment->setValue(QLatin1String("left")); 0078 break; 0079 } 0080 } 0081 0082 int Text::verticalAlignment() const 0083 { 0084 const QString a = m_text->m_horizontalAlignment->value().toString().toLower(); 0085 0086 if (a == QLatin1String("top")) { 0087 return -1; 0088 } 0089 if (a == QLatin1String("middle")) { 0090 return 0; 0091 } 0092 if (a == QLatin1String("bottom")) { 0093 return 1; 0094 } 0095 return -1; 0096 } 0097 void Text::setVerticalAlignment(int a) 0098 { 0099 switch (a) { 0100 case -1: 0101 m_text->m_verticalAlignment->setValue(QLatin1String("top")); 0102 break; 0103 case 0: 0104 m_text->m_verticalAlignment->setValue(QLatin1String("middle")); 0105 break; 0106 case 1: 0107 m_text->m_verticalAlignment->setValue(QLatin1String("bottom")); 0108 break; 0109 default: 0110 m_text->m_verticalAlignment->setValue(QLatin1String("middle")); 0111 break; 0112 } 0113 } 0114 0115 QColor Text::backgroundColor() const 0116 { 0117 return m_text->m_backgroundColor->value().value<QColor>(); 0118 } 0119 void Text::setBackgroundColor(const QColor& c) 0120 { 0121 m_text->m_backgroundColor->setValue(QColor(c)); 0122 } 0123 0124 QColor Text::foregroundColor() const 0125 { 0126 return m_text->m_foregroundColor->value().value<QColor>(); 0127 } 0128 void Text::setForegroundColor(const QColor& c) 0129 { 0130 m_text->m_foregroundColor->setValue(QColor(c)); 0131 } 0132 0133 int Text::backgroundOpacity() const 0134 { 0135 return m_text->m_backgroundOpacity->value().toInt(); 0136 } 0137 void Text::setBackgroundOpacity(int o) 0138 { 0139 m_text->m_backgroundOpacity->setValue(o); 0140 } 0141 0142 QColor Text::lineColor() const 0143 { 0144 return m_text->m_lineColor->value().value<QColor>(); 0145 } 0146 void Text::setLineColor(const QColor& c) 0147 { 0148 m_text->m_lineColor->setValue(QColor(c)); 0149 } 0150 0151 int Text::lineWeight() const 0152 { 0153 return m_text->m_lineWeight->value().toInt(); 0154 } 0155 void Text::setLineWeight(int w) 0156 { 0157 m_text->m_lineWeight->setValue(w); 0158 } 0159 0160 int Text::lineStyle() const 0161 { 0162 return m_text->m_lineStyle->value().toInt(); 0163 } 0164 void Text::setLineStyle(int s) 0165 { 0166 if (s < 0 || s > 5) { 0167 s = 1; 0168 } 0169 m_text->m_lineStyle->setValue(s); 0170 } 0171 0172 QPointF Text::position() const 0173 { 0174 return m_text->position(); 0175 } 0176 void Text::setPosition(const QPointF& p) 0177 { 0178 m_text->setPosition(p); 0179 } 0180 0181 QSizeF Text::size() const 0182 { 0183 return m_text->size(); 0184 } 0185 void Text::setSize(const QSizeF& s) 0186 { 0187 m_text->setSize(s); 0188 } 0189 0190 bool Text::loadFromFile(const QString &fileName) 0191 { 0192 QFile file(fileName); 0193 //kreportpluginDebug() << "Loading from" << fn; 0194 if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { 0195 kreportpluginWarning() << "Failed to load value for text element from file" << fileName; 0196 return false; 0197 } 0198 QTextStream in(&file); 0199 QString data = in.readAll(); 0200 /* 0201 while (!in.atEnd()) { 0202 QString line = in.readLine(); 0203 process_line(line); 0204 }*/ 0205 m_text->m_itemValue->setValue(data); 0206 return true; 0207 } 0208 0209 }