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

0001 /***************************************************************************
0002                           dlgmaproomproperties.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 "dlgmaproomproperties.h"
0019 
0020 #include "../cmaproom.h"
0021 #include "../cmappath.h"
0022 #include "../cmapmanager.h"
0023 #include "../cmapcmdelementcreate.h"
0024 #include "../cmapcmdelementdelete.h"
0025 #include "../cmapcmdelementproperties.h"
0026 #include "../cmapcmdgroup.h"
0027 #include "../cmappluginbase.h"
0028 #include "../cmappropertiespanebase.h"
0029 
0030 #include <qlineedit.h>
0031 #include <qcheckbox.h>
0032 #include <qlabel.h>
0033 #include <qpushbutton.h>
0034 #include <qlistwidget.h>
0035 #include <qtabwidget.h>
0036 #include <qtreewidget.h>
0037 #include <QDebug>
0038 
0039 #include <kcolorbutton.h>
0040 #include <KLocalizedString>
0041 
0042 DlgMapRoomProperties::DlgMapRoomProperties(CMapManager *manager,CMapRoom *roomElement,QWidget *parent )
0043     : QDialog(parent)
0044 {
0045   setupUi (this);
0046   connect(this, SIGNAL(accepted()), this, SLOT(slotAccept()));
0047 
0048     room = roomElement;
0049     mapManager = manager;
0050 
0051     // Populate the dialog
0052     txtLabel->setText(room->getLabel());
0053     txtDescription->setText(room->getDescription());
0054     cmdRoomColor->setColor(room->getColor());
0055     slotUseDefaultColor(room->getUseDefaultCol());
0056     setLabelPos(room->getLabelPosition());
0057 
0058     lstContents->addItems(*room->getContentsList());
0059 
0060     regenerateExits();
0061 
0062     // Get the extension panels from the plugins
0063     QList<CMapPropertiesPaneBase *> paneList = mapManager->createPropertyPanes(ROOM,(CMapElement*)roomElement,(QWidget *)RoomsTab);
0064     foreach (CMapPropertiesPaneBase *pane, paneList)
0065     {
0066         RoomsTab->addTab(pane,pane->getTitle());
0067         connect(cmdOk,SIGNAL(clicked()),pane,SLOT(slotOk()));
0068         connect(cmdCancel,SIGNAL(clicked()),pane,SLOT(slotCancel()));
0069     }
0070 }
0071 
0072 DlgMapRoomProperties::~DlgMapRoomProperties()
0073 {
0074 }
0075 
0076 void DlgMapRoomProperties::regenerateExits(void)
0077 {
0078   lstPaths->clear();
0079   QTreeWidgetItem *item = nullptr;
0080   foreach (CMapPath *path, *room->getPathList())
0081   {
0082     QString direction = mapManager->directionToText(path->getSrcDir(),path->getSpecialCmd());
0083     qDebug() << "Path : " << path->getSrcDir() << "," << path->getSpecialCmd() << "," << direction; 
0084 
0085     item = new QTreeWidgetItem();
0086     item->setText(0, direction);
0087     item->setText(1, path->getBeforeCommand());
0088     item->setText(2, path->getAfterCommand());
0089     lstPaths->addTopLevelItem(item);
0090   }
0091 
0092   if (item) lstPaths->setCurrentItem(item);
0093 }
0094 
0095 void DlgMapRoomProperties::slotAccept()
0096 {
0097   CMapCmdElementProperties *command = new CMapCmdElementProperties(mapManager,i18n("Changed Room Properties"),room);
0098 
0099   command->compare("Label",room->getLabel(),txtLabel->text().trimmed());
0100   command->compare("Description",room->getDescription(),txtDescription->toPlainText().trimmed());
0101   command->compare("Color",room->getColor(),cmdRoomColor->color());
0102   command->compare("DefaultColor",room->getUseDefaultCol(),chkUseDefaltColor->isChecked());
0103   command->compare("LabelPos",(int)room->getLabelPosition(),(int)getLabelPos());
0104 
0105   QStringList newContents;
0106   for (int i = 0; i < lstContents->count(); ++i)
0107   {
0108     QString text = lstContents->item(i)->text().trimmed();
0109     if (text.length()) newContents += text;
0110   }
0111 
0112   command->compare("Contents",*room->getContentsList(),newContents);
0113 
0114   QList<CMapPath*> wipe;
0115   foreach (CMapPath *path, *room->getPathList())
0116   {
0117     QString name = mapManager->directionToText(path->getSrcDir(),path->getSpecialCmd());
0118 
0119     bool found = false;
0120 
0121     for (int i = 0; i < lstPaths->topLevelItemCount(); ++i)
0122       if (lstPaths->topLevelItem(i)->text(0) == name)
0123         found = true;
0124     if (!found)
0125       wipe.push_back(path);
0126   }
0127   foreach (CMapPath *path, wipe)
0128     mapManager->deleteElement(path);
0129 
0130   mapManager->addCommand(command);
0131 }
0132 
0133 void DlgMapRoomProperties::slotUseDefaultColor(bool useDefaultColor)
0134 {
0135     chkUseDefaltColor->setChecked(useDefaultColor);
0136     
0137     cmdRoomColor->setEnabled(!useDefaultColor);
0138     lblRoomColor->setEnabled(!useDefaultColor);
0139 }
0140 
0141 void DlgMapRoomProperties::setLabelPos(CMapRoom::labelPosTyp position)
0142 {
0143     cmdN->setChecked(false);
0144     cmdNE->setChecked(false);
0145     cmdE->setChecked(false);
0146     cmdSE->setChecked(false);
0147     cmdS->setChecked(false);
0148     cmdSW->setChecked(false);
0149     cmdW->setChecked(false);
0150     cmdNW->setChecked(false);
0151     cmdHide->setChecked(false);
0152     cmdCustom->setChecked(false);
0153 
0154     switch (position)
0155     {
0156         case CMapRoom::NORTH     : cmdN->setChecked(true); break;
0157         case CMapRoom::NORTHEAST : cmdNE->setChecked(true); break;
0158         case CMapRoom::EAST      : cmdE->setChecked(true); break;
0159         case CMapRoom::SOUTHEAST : cmdSE->setChecked(true); break;
0160         case CMapRoom::SOUTH     : cmdS->setChecked(true); break;
0161         case CMapRoom::SOUTHWEST : cmdSW->setChecked(true); break;
0162         case CMapRoom::WEST      : cmdW->setChecked(true); break;
0163         case CMapRoom::NORTHWEST : cmdNW->setChecked(true); break;
0164         case CMapRoom::HIDE      : cmdHide->setChecked(true); break;
0165         case CMapRoom::CUSTOM    : cmdCustom->setChecked(true); break;
0166     }
0167 }
0168 
0169 CMapRoom::labelPosTyp DlgMapRoomProperties::getLabelPos(void)
0170 {
0171     if (cmdN->isChecked())
0172         return CMapRoom::NORTH;
0173 
0174     if (cmdE->isChecked())
0175         return CMapRoom::EAST;
0176 
0177     if (cmdS->isChecked())
0178         return CMapRoom::SOUTH;
0179 
0180     if (cmdW->isChecked())
0181         return CMapRoom::WEST;
0182 
0183     if (cmdNE->isChecked())
0184         return CMapRoom::NORTHEAST;
0185 
0186     if (cmdSE->isChecked())
0187         return CMapRoom::SOUTHEAST;
0188 
0189     if (cmdSW->isChecked())
0190         return CMapRoom::SOUTHWEST;
0191 
0192     if (cmdNW->isChecked())
0193         return CMapRoom::NORTHWEST;
0194 
0195     if (cmdHide->isChecked())
0196         return CMapRoom::HIDE;
0197 
0198     if (cmdCustom->isChecked())
0199         return CMapRoom::CUSTOM;
0200 
0201     return CMapRoom::HIDE;
0202 }
0203 
0204 void DlgMapRoomProperties::slotE()
0205 {
0206     setLabelPos(CMapRoom::EAST);
0207 }
0208 
0209 void DlgMapRoomProperties::slotHide()
0210 {
0211     setLabelPos(CMapRoom::HIDE);
0212 }
0213 
0214 void DlgMapRoomProperties::slotN()
0215 {
0216     setLabelPos(CMapRoom::NORTH);
0217 }
0218 
0219 void DlgMapRoomProperties::slotNE()
0220 {
0221     setLabelPos(CMapRoom::NORTHEAST);
0222 }
0223 
0224 void DlgMapRoomProperties::slotNW()
0225 {
0226     setLabelPos(CMapRoom::NORTHWEST);
0227 }
0228 
0229 void DlgMapRoomProperties::slotS()
0230 {
0231     setLabelPos(CMapRoom::SOUTH);
0232 }
0233 
0234 void DlgMapRoomProperties::slotSE()
0235 {
0236     setLabelPos(CMapRoom::SOUTHEAST);
0237 }
0238 
0239 void DlgMapRoomProperties::slotSW()
0240 {
0241     setLabelPos(CMapRoom::SOUTHWEST);
0242 }
0243 
0244 void DlgMapRoomProperties::slotW()
0245 {
0246     setLabelPos(CMapRoom::WEST);
0247 }
0248 
0249 void DlgMapRoomProperties::slotCustom()
0250 {
0251     setLabelPos(CMapRoom::CUSTOM);
0252 }
0253 
0254 void DlgMapRoomProperties::slotRemoveItem()
0255 {
0256   QListWidgetItem *item = lstContents->takeItem(lstContents->currentRow());
0257   if (item) delete item;
0258 }
0259 
0260 void DlgMapRoomProperties::slotAddItem()
0261 {
0262   lstContents->addItem(QString());
0263   lstContents->setCurrentRow(lstContents->count() - 1);
0264 }
0265 
0266 void DlgMapRoomProperties::slotNewItemSelected()
0267 {
0268   QList<QListWidgetItem *> sel = lstContents->selectedItems();
0269   if (!sel.count()) return;
0270   txtItemName->setText(sel.at(0)->text());
0271 }
0272 
0273 void DlgMapRoomProperties::slotEditItemName(const QString & name)
0274 {
0275   QListWidgetItem *item = lstContents->currentItem();
0276   if (item) item->setText(name);
0277 }
0278 
0279 void DlgMapRoomProperties::slotPathDelete()
0280 {
0281   QList<QTreeWidgetItem *> sel = lstPaths->selectedItems();
0282   foreach (QTreeWidgetItem *item, sel)
0283     delete item;
0284 }
0285 
0286 void DlgMapRoomProperties::slotPathProperties()
0287 {
0288   QTreeWidgetItem *item = lstPaths->currentItem();
0289   int idx = lstPaths->indexOfTopLevelItem(item);
0290   if ((idx < 0) || (idx >= room->getPathList()->count())) return;
0291   CMapPath *path = room->getPathList()->at(idx);
0292 
0293   mapManager->propertiesPath(path);
0294 
0295   item->setText(0, mapManager->directionToText(path->getSrcDir(),path->getSpecialCmd()));
0296   item->setText(1, path->getBeforeCommand());
0297   item->setText(2, path->getAfterCommand());
0298 }
0299 
0300 #include "moc_dlgmaproomproperties.cpp"