selectdiagramwidget.cpp 2.71 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/***************************************************************************
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   copyright (C) 2019                                                    *
 *   Umbrello UML Modeller Authors <umbrello-devel@kde.org>                *
 ***************************************************************************/

#include "selectdiagramwidget.h"

#include "uml.h"
#include "umldoc.h"
#include "umlscene.h"
#include "umlview.h"

#include <KComboBox>
#include <KLocalizedString>

#include <QLabel>
#include <QHBoxLayout>

/**
 * constructor
 * @param title title shown as label
 * @param parent parent widget
 */
SelectDiagramWidget::SelectDiagramWidget(const QString &title, QWidget *parent)
30
  : ComboBoxWidgetBase(title, QString(), parent)
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
{
}

/**
 * setup widget woth diagram type, currently used diagram, diagram name to exclude and an option to choose a new diagram
 * @param type
 * @param currentName
 * @param excludeName
 * @param withNewEntry
 */
void SelectDiagramWidget::setupWidget(Uml::DiagramType::Enum type, const QString &currentName, const QString &excludeName, bool withNewEntry)
{
    QStringList diagrams;

    foreach (UMLView *view, UMLApp::app()->document()->viewIterator()) {
        QString name = view->umlScene()->name();
        if (name == excludeName)
            continue;
49
        if (type == Uml::DiagramType::Undefined || type == view->umlScene()->type()) {
50 51
            diagrams << name;
            m_editField->completionObject()->addItem(name);
52
        }
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
    }
    diagrams.sort();
    if (withNewEntry)
        diagrams.push_front(i18n("New"));
    m_editField->clear();
    m_editField->insertItems(-1, diagrams);
    int currentIndex = m_editField->findText(currentName);
    if (currentIndex > -1) {
        m_editField->setCurrentIndex(currentIndex);
    }
}

/**
 * Return current text
 * @return The text entered by the user
 */
QString SelectDiagramWidget::currentText()
{
    return m_editField->currentText();
}

/**
 * Return the id of the currently selected diagram
 * @return id of diagram
 * @return Uml::ID::None in case the diagram was not found
 */
Uml::ID::Type SelectDiagramWidget::currentID()
{
    QString name = m_editField->currentText();
    foreach (UMLView *view, UMLApp::app()->document()->viewIterator()) {
        if (name == view->umlScene()->name())
            return view->umlScene()->ID();
    }
    return Uml::ID::None;
}