File indexing completed on 2024-05-19 09:39:40

0001 /***************************************************************************
0002  *   Copyright (C) 2005 by David Saxton                                    *
0003  *   david@bluehaze.org                                                    *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  ***************************************************************************/
0010 
0011 #include "icnview.h"
0012 #include "canvasmanipulator.h"
0013 #include "icndocument.h"
0014 #include "ktechlab.h"
0015 
0016 #include <KActionCollection>
0017 #include <KLocalizedString>
0018 #include <KToolBarPopupAction>
0019 
0020 #include <QAction>
0021 #include <QActionGroup>
0022 #include <QMenu>
0023 
0024 ICNView::ICNView(ICNDocument *icnDocument, ViewContainer *viewContainer, uint viewAreaId)
0025     : ItemView(icnDocument, viewContainer, viewAreaId)
0026 {
0027     bool manualRouting = (icnDocument->m_cmManager->cmState() & CMManager::cms_manual_route);
0028 
0029     KActionCollection *ac = actionCollection();
0030 
0031     // BEGIN Routing Actions
0032     // These actions get inserted into the main menu
0033     // m_pAutoRoutingAction = new QAction( i18n("Automatic"), "", 0, this, SLOT(slotSetRoutingAuto()), ac, "routing_mode_auto" );
0034     m_pAutoRoutingAction = new QAction(i18n("Automatic"), ac);
0035     m_pAutoRoutingAction->setObjectName("routing_mode_auto");
0036     connect(m_pAutoRoutingAction, &QAction::triggered, this, &ICNView::slotSetRoutingAuto);
0037     ac->addAction(m_pAutoRoutingAction->objectName(), m_pAutoRoutingAction);
0038     // m_pAutoRoutingAction->setExclusiveGroup("routing_mode");// TODO TEST
0039     if (!manualRouting)
0040         m_pAutoRoutingAction->setChecked(true);
0041 
0042     // m_pManualRoutingAction = new QAction( i18n("Manual"), "", 0, this, SLOT(slotSetRoutingManual()), ac, "routing_mode_manual" );
0043     m_pManualRoutingAction = new QAction(i18n("Manual"), ac);
0044     m_pManualRoutingAction->setObjectName("routing_mode_manual");
0045     connect(m_pManualRoutingAction, &QAction::triggered, this, &ICNView::slotSetRoutingManual);
0046     ac->addAction(m_pManualRoutingAction->objectName(), m_pManualRoutingAction);
0047     // m_pManualRoutingAction->setExclusiveGroup("routing_mode"); // TODO TEST
0048     if (manualRouting)
0049         m_pManualRoutingAction->setChecked(true);
0050 
0051     // This popup gets inserted into the toolbar
0052     // m_pRoutingModeToolbarPopup = new KToolBarPopupAction( i18n("Connection Routing Mode"), "pencil", 0, 0, 0, ac, "routing_mode" );
0053     m_pRoutingModeToolbarPopup = new KToolBarPopupAction(QIcon(QString::fromLatin1("pencil")), i18n("Connection Routing Mode"), ac);
0054     m_pRoutingModeToolbarPopup->setObjectName("routing_mode");
0055     m_pRoutingModeToolbarPopup->setDelayed(false);
0056     ac->addAction(m_pRoutingModeToolbarPopup->objectName(), m_pRoutingModeToolbarPopup);
0057 
0058     QMenu *m = m_pRoutingModeToolbarPopup->menu();
0059     m->setTitle(i18n("Connection Routing Mode"));
0060 
0061     m_actMenuRouteAutoRoute = m->addAction(/*QIconLoader::global()->loadIcon(
0062        "routing_mode_auto", QIconLoader::Small ), */
0063                                            i18n("Automatic"));
0064     m_actMenuRouteAutoRoute->setData(0);
0065     m_actMenuRouteManRoute = m->addAction(/*QIconLoader::global()->loadIcon(
0066        "routing_mode_manual",   QIconLoader::Small ),*/
0067                                           i18n("Manual"));
0068     m_actMenuRouteManRoute->setData(1);
0069 
0070     // m->setCheckable(true); // 2018.12.02
0071     if (manualRouting) {
0072         m_actMenuRouteManRoute->setChecked(true);
0073     } else {
0074         m_actMenuRouteAutoRoute->setChecked(true);
0075     }
0076     // m->setItemChecked( manualRouting ? 1 : 0, true ); // 2018.12.02
0077 
0078     connect(m, &QMenu::triggered, this, &ICNView::slotSetRoutingMode);
0079     // END Routing Actions
0080 
0081     connect(icnDocument->m_cmManager, &CMManager::manualRoutingChanged, this, &ICNView::slotUpdateRoutingToggles);
0082 }
0083 
0084 ICNView::~ICNView()
0085 {
0086 }
0087 
0088 void ICNView::slotSetRoutingMode(QAction *action)
0089 {
0090     int mode = action->data().toInt();
0091     // This function is called when the user selects a mode from the toolbar drop-down menu
0092     bool manualEnabled = (mode == 1);
0093 
0094     if (bool(p_itemDocument->m_cmManager->cmState() & CMManager::cms_manual_route) == manualEnabled)
0095         return;
0096 
0097     slotUpdateRoutingMode(manualEnabled);
0098     slotUpdateRoutingToggles(manualEnabled);
0099 }
0100 
0101 void ICNView::slotSetRoutingManual()
0102 {
0103     slotUpdateRoutingMode(true);
0104     slotUpdateRoutingToggles(true);
0105 }
0106 
0107 void ICNView::slotSetRoutingAuto()
0108 {
0109     slotUpdateRoutingMode(false);
0110     slotUpdateRoutingToggles(false);
0111 }
0112 
0113 void ICNView::slotUpdateRoutingMode(bool manualRouting)
0114 {
0115     p_itemDocument->m_cmManager->slotSetManualRoute(manualRouting);
0116     p_itemDocument->canvas()->setMessage(manualRouting ? i18n("<b>Manual</b> connection routing enabled.") : i18n("<b>Automatic</b> connection routing enabled."));
0117 }
0118 
0119 void ICNView::slotUpdateRoutingToggles(bool manualRouting)
0120 {
0121     // m_pRoutingModeToolbarPopup->menu()->setItemChecked( !manualRouting, 0 );  // 2018.12.02
0122     // m_pRoutingModeToolbarPopup->menu()->setItemChecked(  manualRouting, 1 );
0123     if (manualRouting) {
0124         m_actMenuRouteAutoRoute->setChecked(false);
0125         m_actMenuRouteManRoute->setChecked(true);
0126     } else {
0127         m_actMenuRouteAutoRoute->setChecked(true);
0128         m_actMenuRouteManRoute->setChecked(false);
0129     }
0130 
0131     if (manualRouting)
0132         m_pManualRoutingAction->setChecked(true);
0133     else
0134         m_pAutoRoutingAction->setChecked(true);
0135 }
0136 
0137 #include "moc_icnview.cpp"