File indexing completed on 2024-04-28 04:02:52

0001 /***************************************************************************
0002                           dlgmaptextproperties.cpp  -  description
0003                              -------------------
0004     begin                : Thu Mar 8 2001
0005     copyright            : (C) 2001 by KMud Development Team
0006     email                : kmud-devel@kmud.de
0007  ***************************************************************************/
0008 
0009 /***************************************************************************
0010  *                                                                         *
0011  *   This program is free software; you can redistribute it and/or modify  *
0012  *   it under the terms of the GNU General Public License as published by  *
0013  *   the Free Software Foundation; either version 2 of the License, or     *
0014  *   (at your option) any later version.                                   *
0015  *                                                                         *
0016  ***************************************************************************/
0017 
0018 #include "dlgmaptextproperties.h"
0019 
0020 #include <qfile.h>
0021 #include <qcheckbox.h>
0022 #include <qlabel.h>
0023 #include <qlineedit.h>
0024 #include <qpalette.h>
0025 #include <qfontdatabase.h>
0026 #include <qtabwidget.h>
0027 #include <qlayout.h>
0028 #include <qfontmetrics.h>
0029 #include <QScrollArea>
0030 #include <QPixmap>
0031 #include <QVBoxLayout>
0032 #include <QDebug>
0033 
0034 #include <kcolorbutton.h>
0035 #include <KLocalizedString>
0036 
0037 #include "../cmaptext.h"
0038 #include "../cmapmanager.h"
0039 #include "../cmapview.h"
0040 #include "../cmapcmdelementcreate.h"
0041 #include "../cmapcmdelementdelete.h"
0042 #include "../cmapcmdelementproperties.h"
0043 #include "../cmapcmdgroup.h"
0044 
0045 #include "../cmappluginbase.h"
0046 #include "../cmappropertiespanebase.h"
0047 
0048 CMapTextPreview::CMapTextPreview(CMapManager *manager,QWidget *parent)
0049     : QWidget(parent)
0050 {
0051   setAttribute (Qt::WA_StaticContents);
0052   buffer = nullptr;
0053   mapManager = manager;
0054 }
0055 
0056 CMapTextPreview::~CMapTextPreview()
0057 {
0058     if (buffer)
0059         delete buffer;
0060 }
0061 
0062 void CMapTextPreview::drawContents(QPainter *paint,int , int , int, int )
0063 {
0064     QRect drawArea(0,0,width(),height());
0065     
0066     // delete the buffer only when we need one with a different size
0067     if (buffer && (buffer->size() != drawArea.size()))
0068     {
0069         delete buffer;
0070         buffer = nullptr;
0071     }
0072 
0073     if (!buffer)
0074     {
0075         buffer = new QPixmap(drawArea.size());
0076     }
0077 
0078     QPainter p;
0079 
0080     p.begin(buffer);
0081 
0082     if (mapManager->getActiveView()->getCurrentlyViewedZone()->getUseDefaultBackground())
0083     {
0084         p.fillRect(drawArea,mapManager->getMapData()->backgroundColor);
0085     }
0086     else
0087     {
0088         p.fillRect(drawArea,mapManager->getActiveView()->getCurrentlyViewedZone()->getBackgroundColor());
0089     }
0090 
0091     QStringList textList;
0092     CMapText::stringToList(text,&textList);
0093     CMapText::paintText(&p,color,QPoint(0,0),font,&textList,size);
0094 
0095     paint->drawPixmap(0,0,*buffer);
0096 }
0097 
0098 
0099 DlgMapTextProperties::DlgMapTextProperties(CMapManager *manager,CMapText *textElement,QWidget *parent) : QDialog(parent)
0100 {
0101   setupUi (this);
0102 
0103     text = textElement;
0104     mapManager = manager;
0105     QVBoxLayout *vbox = new QVBoxLayout((QWidget *)fraPreview);
0106     textScrollView = new CMapTextPreview(mapManager,fraPreview);
0107         QScrollArea *textScrollArea = new QScrollArea (fraPreview);
0108         textScrollArea->setWidget (textScrollView);
0109     vbox->addWidget( textScrollArea);
0110     textScrollView->show();
0111     fillFamilyList();
0112     setFont(text->getFont());
0113     QString width = QString::number (text->getWidth());
0114     QString height = QString::number (text->getHeight());
0115     txtText->setText(text->getText());
0116     txtWidth->setText(width);
0117     txtHeight->setText(height);
0118     cmdColor->setColor(text->getColor());
0119 
0120     //FIXME_jp: set txtText background to background color of the map
0121 
0122     // Get the extension panels from the plugins
0123     QList<CMapPropertiesPaneBase *> paneList = mapManager->createPropertyPanes(TEXT,(CMapElement*)textElement,(QWidget *)TextTabs);
0124     foreach (CMapPropertiesPaneBase *pane, paneList)
0125     {
0126         TextTabs->addTab(pane,pane->getTitle());
0127         connect(cmdOk,SIGNAL(clicked()),pane,SLOT(slotOk()));
0128         connect(cmdCancel,SIGNAL(clicked()),pane,SLOT(slotCancel()));
0129     }
0130 
0131     slotUpdatePreview();
0132 }
0133 
0134 DlgMapTextProperties::~DlgMapTextProperties()
0135 {
0136 }
0137 
0138 void DlgMapTextProperties::fillFamilyList(void)
0139 {
0140   lstFamily->insertItems (0, QFontDatabase().families());
0141 }
0142 
0143 void DlgMapTextProperties::setFont(QFont font)
0144 {
0145     textFont = font;
0146     
0147     QString family = font.family();
0148     family = family.trimmed();
0149     QString size = QString::number(font.pointSize());
0150     
0151     for (int i = 0 ; i<lstFamily->count();i++)
0152     {
0153         QString s = lstFamily->item(i)->text();
0154         if (s == family)
0155         {
0156             lstFamily->setCurrentRow(i);
0157             break;
0158         }
0159     }   
0160 
0161     chkBold->setChecked(font.bold());
0162     chkItalic->setChecked(font.italic());
0163     lstFamily->scrollToItem(lstFamily->currentItem());
0164     slotUpdatePreview();
0165 }
0166 
0167 void DlgMapTextProperties::slotSetSize(void)
0168 {
0169     qDebug() << "CMapTextPreview::slotSetSize1 ";
0170     int fontSize = txtFontSize->text().toInt();
0171     textFont.setPointSize(fontSize);
0172     QFontMetrics fm(textFont);  
0173     QStringList textList;
0174     CMapText::stringToList(txtText->toPlainText(),&textList);
0175     int tmpWidth = 0;
0176     for (QStringList::iterator it = textList.begin(); it != textList.end(); ++it)
0177     {
0178         if (fm.horizontalAdvance(*it) > tmpWidth)
0179             tmpWidth = fm.horizontalAdvance(*it);
0180     }
0181 
0182     QString width = QString::number(tmpWidth);
0183     QString height = QString::number(fm.height() * textList.count());
0184     txtWidth->setText(width);
0185     txtHeight->setText(height);
0186     txtFontSize->setText("");
0187     slotUpdatePreview();
0188 }
0189 
0190 void DlgMapTextProperties::slotBoldClicked(void)
0191 {
0192     textFont.setBold(chkBold->isChecked());
0193     slotUpdatePreview();
0194 }
0195 
0196 void DlgMapTextProperties::slotItalicClicked(void)
0197 {
0198     textFont.setItalic(chkItalic->isChecked());
0199     slotUpdatePreview();
0200 }
0201 
0202 void DlgMapTextProperties::slotFamilySelected()                     
0203 {
0204     textFont.setFamily(lstFamily->currentItem()->text());
0205     slotUpdatePreview();
0206 }
0207 
0208 void DlgMapTextProperties::slotColorChanged(const QColor &color)
0209 {
0210     textColor = color;
0211     slotUpdatePreview();
0212 }
0213 
0214 void DlgMapTextProperties::slotAccept()
0215 {
0216     CMapCmdElementProperties *command = new CMapCmdElementProperties(mapManager,i18n("Changed Room Properties"),text);
0217 
0218     QStringList textList;
0219     int width = txtWidth->text().toInt();
0220     int height = txtHeight->text().toInt();
0221 
0222     command->compare("Text",text->getText(),txtText->toPlainText());
0223     command->compare("Color",text->getColor(),textColor);
0224     command->compare("Font",text->getFont(),textFont);
0225     command->compare("Size",text->getSize(),QSize(width,height));
0226 
0227     mapManager->addCommand(command);
0228 }
0229 
0230 void DlgMapTextProperties::slotUpdatePreview()
0231 {
0232     int gridWidth = mapManager->getMapData()->gridSize.width();
0233     int gridHeight = mapManager->getMapData()->gridSize.height();
0234     int width =txtWidth->text().toInt();
0235     if (width<gridWidth)
0236         width = gridWidth;
0237     int height = txtHeight->text().toInt();
0238     if (height < gridHeight)
0239         height = 20;
0240 
0241     textScrollView->setFont(textFont);
0242     textScrollView->setColor(textColor);
0243     textScrollView->setSize(QSize(width,height));
0244     textScrollView->setText(txtText->toPlainText());
0245     textScrollView->resize(txtWidth->text().toInt(),txtHeight->text().toInt());
0246     textScrollView->update();
0247 }
0248 
0249 #include "moc_dlgmaptextproperties.cpp"