fleetdlg.cc 5.11 KB
Newer Older
1
/*
2 3 4 5 6
    Copyright 2003 Russell Steffen <rsteffen@bayarea.net>
    Copyright 2003 Stephan Zehetner <s.zehetner@nevox.org>
    Copyright 2006 Dmitry Suzdalev <dimsuz@gmail.com>
    Copyright 2006 Inge Wallin <inge@lysator.liu.se>
    Copyright 2006 Pierre Ducroquet <pinaraf@gmail.com>
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

    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.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */
22

23 24
#include "fleetdlg.h"

Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
25 26 27
#include <QTableWidget>
#include <QHeaderView>

Matthias Kretz's avatar
Matthias Kretz committed
28
#include <kcomponentdata.h>
29
#include <kglobal.h>
Stephan Binner's avatar
Stephan Binner committed
30
#include <kpushbutton.h>
André Wöbbeking's avatar
André Wöbbeking committed
31
#include <KStandardGuiItem>
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
32
#include <kguiitem.h>
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
33
#include <klocale.h>
Russell Steffen's avatar
 
Russell Steffen committed
34

35
#include "planet.h"
Russell Steffen's avatar
 
Russell Steffen committed
36

Pierre Ducroquet's avatar
Pierre Ducroquet committed
37 38
FleetDlg::FleetDlg( QWidget *parent,
                    const AttackFleetList &fleets,
Jeffrey Kelling's avatar
Jeffrey Kelling committed
39 40 41
                    const AttackFleetList &newFleets,
                    const AttackFleetList &standingOrders)
    : KDialog(parent), m_newFleetList(newFleets), m_standingOrders(standingOrders), m_fleetList(fleets)
Russell Steffen's avatar
 
Russell Steffen committed
42
{
43
    setObjectName( QLatin1String( "FleetDlg" ) );
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
44
    setModal( true );
Aaron J. Seigo's avatar
Aaron J. Seigo committed
45
    setCaption( i18n("Fleet Overview") );
46
    setButtons( KDialog::Ok );
Russell Steffen's avatar
 
Russell Steffen committed
47

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
    m_fleetTable = new QTableWidget(this);
    m_fleetTable->setColumnCount(7);
    m_fleetTable->setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);
    m_fleetTable->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
    m_fleetTable->setSelectionMode(QAbstractItemView::NoSelection);

    QStringList headerLabels;
    headerLabels
        << QString()
        << i18n("Fleet No.")
        << i18n("Source")
        << i18n("Destination")
        << i18n("Ships")
        << i18n("Kill Percentage")
        << i18n("Arrival Turn");
    m_fleetTable->setHorizontalHeaderLabels(headerLabels);
Inge Wallin's avatar
Inge Wallin committed
64
    m_fleetTable->verticalHeader()->hide();
65

Inge Wallin's avatar
Inge Wallin committed
66
    m_fleetTable->setMinimumSize( m_fleetTable->sizeHint() );
Russell Steffen's avatar
 
Russell Steffen committed
67

68
    setMainWidget( m_fleetTable );
Laurent Montel's avatar
Laurent Montel committed
69
    connect( this, SIGNAL(okClicked()), this, SLOT(accept()) );
Russell Steffen's avatar
 
Russell Steffen committed
70 71 72 73

    init();

    resize( 580, 140  );
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
74

Inge Wallin's avatar
Inge Wallin committed
75
    m_fleetTable->setSortingEnabled(true);
76
    m_fleetTable->sortItems( 1, Qt::AscendingOrder );
Russell Steffen's avatar
 
Russell Steffen committed
77 78
}

79

Russell Steffen's avatar
 
Russell Steffen committed
80
void
81
FleetDlg::init()
Russell Steffen's avatar
 
Russell Steffen committed
82
{
Pierre Ducroquet's avatar
Pierre Ducroquet committed
83
    AttackFleet *curFleet=0;
Jeffrey Kelling's avatar
Jeffrey Kelling committed
84 85 86
    AttackFleetList fleets = m_standingOrders + m_newFleetList + m_fleetList;
    const int standingOrders = m_standingOrders.count();
    const int newFleets = standingOrders + m_newFleetList.count();
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
87

88
    m_fleetTable->setRowCount( fleets.count() );
Pierre Ducroquet's avatar
Pierre Ducroquet committed
89
    QTableWidgetItem *item;
Alexander Schuch's avatar
Alexander Schuch committed
90

91 92
    for( int f = 0; f < fleets.count(); ++f) {
        curFleet = fleets.at(f);
Alexander Schuch's avatar
Alexander Schuch committed
93

94 95 96 97
        item = new QTableWidgetItem();
        if( f < newFleets) {
          item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsUserCheckable);
          item->setCheckState(Qt::Checked);
Jeffrey Kelling's avatar
Jeffrey Kelling committed
98 99
          if( f < standingOrders )
              item->setText(i18n("Standing order"));
100 101 102
        } else {
          item->setFlags(Qt::ItemIsEnabled);
        }
Inge Wallin's avatar
Inge Wallin committed
103
        m_fleetTable->setItem( f, 0, item );
104 105 106

        item = new QTableWidgetItem(QString::number(f + 1));
        item->setFlags(Qt::ItemIsEnabled);
Inge Wallin's avatar
Inge Wallin committed
107
        m_fleetTable->setItem( f, 1, item );
Alexander Schuch's avatar
Alexander Schuch committed
108

109
        item = new QTableWidgetItem(curFleet->source->name());
110
        item->setFlags(Qt::ItemIsEnabled);
Inge Wallin's avatar
Inge Wallin committed
111
        m_fleetTable->setItem( f, 2, item );
Alexander Schuch's avatar
Alexander Schuch committed
112

113
        item = new QTableWidgetItem(curFleet->destination->name());
114
        item->setFlags(Qt::ItemIsEnabled);
Inge Wallin's avatar
Inge Wallin committed
115
        m_fleetTable->setItem( f, 3, item );
Alexander Schuch's avatar
Alexander Schuch committed
116

117
        item = new QTableWidgetItem(QString::number(curFleet->shipCount()));
118
        item->setFlags(Qt::ItemIsEnabled);
Inge Wallin's avatar
Inge Wallin committed
119
        m_fleetTable->setItem( f, 4, item );
Alexander Schuch's avatar
Alexander Schuch committed
120

121
        item = new QTableWidgetItem(QString("%1") .arg(KGlobal::locale()->formatNumber(curFleet->source->killPercentage(), 3)));
122 123
        item->setFlags(Qt::ItemIsEnabled);
        m_fleetTable->setItem( f, 5, item );
Alexander Schuch's avatar
Alexander Schuch committed
124

125 126 127
        item = new QTableWidgetItem(QString::number(curFleet->arrivalTurn));
        item->setFlags(Qt::ItemIsEnabled);
        m_fleetTable->setItem( f, 6, item );
Russell Steffen's avatar
 
Russell Steffen committed
128 129
    }
}
130 131 132 133 134 135


AttackFleetList *FleetDlg::uncheckedFleets() {
    AttackFleetList *fleets = new AttackFleetList();
    QTableWidgetItem *item;
    int count = m_fleetTable->rowCount();
Alexander Schuch's avatar
Alexander Schuch committed
136

137 138 139 140 141
    for( int f = 0; f < count; ++f) {
        item = m_fleetTable->item(f,0);
        if( (item->flags() & Qt::ItemIsUserCheckable) &&
            (item->checkState() == Qt::Unchecked) ) {

Jeffrey Kelling's avatar
Jeffrey Kelling committed
142 143 144 145 146 147 148 149
            const int pos = m_fleetTable->item(f,1)->text().toInt() - 1;
            if (pos >= 0)
            {
                if(pos < m_standingOrders.count())
                    fleets->append( m_standingOrders.at(pos) );
                else
                    fleets->append( m_newFleetList.at(pos - m_standingOrders.count()) );
            }
150 151 152 153 154
        }
    }

    return fleets;
}