PacketDumpTreeView.cpp

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2005-2007 by Peter Dimov.
00004 
00005 This file is part of Calitko (http://www.calitko.org).
00006 
00007 Calitko is free software; you can redistribute it and/or modify
00008 it under the terms of the GNU General Public License as published by
00009 the Free Software Foundation; either version 2 of the License, or
00010 (at your option) any later version.
00011 
00012 Calitko is distributed in the hope that it will be useful,
00013 but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 GNU General Public License for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Calitko; if not, write to the Free Software
00019 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020 
00021 */
00022 
00023 #include "Qt.h"
00024 #include "PacketDumpTreeView.h"
00025 #include "PacketModel.h"
00026 
00027 using UIs::ManagePacketsDialog;
00028 using UIs::PacketDumpTreeView;
00029 using UIs::PacketDumpTreeViewDelegate;
00030 
00031 void PacketDumpTreeViewDelegate::paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
00032 {
00033     QPalette::ColorGroup cg = option.state & QStyle::State_Enabled
00034                            ? QPalette::Normal : QPalette::Disabled;
00035     QStyleOptionViewItem op;
00036     op = option;
00037     op.palette.setBrush (cg, QPalette::Highlight, QBrush (QColor (169, 165, 150)));
00038 
00039     QItemDelegate::paint (painter, op, index);
00040 }
00041 
00042 bool PacketDumpTreeViewDelegate::event (QEvent *e)
00043 {
00044     if (e->type() == QEvent::ToolTip) {
00045         int i;
00046         i=2;
00047     }
00048 
00049     switch (e->type()){
00050     case QEvent::ToolTip:
00051         {
00052             QHelpEvent *hE = static_cast<QHelpEvent *> (e);
00053             QPoint pos = hE->pos();
00054             QToolTip::showText (hE->globalPos(), "proba");
00055             return true;
00056         }
00057     default:
00058         break;
00059     };
00060 
00061     return QItemDelegate::event (e);
00062 }
00063 
00064 ManagePacketsDialog::ManagePacketsDialog(QWidget *parent)
00065  :  QDialog (parent), packetCheckBox(), mainLayout (new QVBoxLayout()),
00066     doneButton (new QPushButton (tr ("Done")))
00067 {
00068     setWindowTitle (tr("Manage Packets Dialog"));
00069 
00070     packetCheckBox[0]   = new QCheckBox (tr ("Ping"), this);
00071     packetCheckBox[1]   = new QCheckBox (tr ("Pong"), this);
00072     packetCheckBox[2]   = new QCheckBox (tr ("Query"), this);
00073     packetCheckBox[3]   = new QCheckBox (tr ("Query Hit"), this);
00074     packetCheckBox[4]   = new QCheckBox (tr ("Query Routing Reset"), this);
00075     packetCheckBox[5]   = new QCheckBox (tr ("Query Routing Patch"), this);
00076 
00077     packetCheckBox[6]   = new QCheckBox (tr ("Incomming"), this);
00078     packetCheckBox[7]   = new QCheckBox (tr ("Outgoing"), this);
00079 
00080     for (int i=0; i<NUM_PACKETS; i++) {
00081         mainLayout->addWidget (packetCheckBox[i]);
00082         if (i==5) mainLayout->addSpacing (10);
00083     }
00084 
00085     QObject::connect (doneButton, SIGNAL (clicked()), this, SLOT (isDone()));
00086 
00087     mainLayout->addWidget (doneButton);
00088 
00089     setLayout (mainLayout);
00090 }
00091 
00092 ManagePacketsDialog::~ManagePacketsDialog()
00093 {
00094     for (int i=0; i<NUM_PACKETS; i++) {
00095         delete packetCheckBox[i];
00096     }
00097 
00098     delete mainLayout;
00099     delete doneButton;
00100 }
00101 
00102 void ManagePacketsDialog::isDone()
00103 {
00104     QDialog::done (0);
00105 }
00106 
00107 PacketFlags ManagePacketsDialog::packetFlags()
00108 {
00109     PacketFlags flags;
00110 
00111     for (int i=0; i<NUM_PACKETS; i++) {
00112         if (packetCheckBox[i]->checkState() == Qt::Checked) {
00113             flags |= Packets(1<<i);
00114         }
00115     }
00116 
00117     return flags;
00118 }
00119 
00120 void ManagePacketsDialog::setPacketFlags (PacketFlags &flags)
00121 {
00122     for (int i=0; i<NUM_PACKETS; i++) {
00123         if (flags & Packets(1<<i)) {
00124             packetCheckBox[i]->setCheckState (Qt::Checked);
00125         }
00126     }
00127 
00128     return;
00129 }
00130 
00131 PacketDumpTreeView::PacketDumpTreeView (QWidget *parent, QAbstractItemModel *)
00132  :  QTreeView (parent), menu (new QMenu ("Searach")),
00133     managePacketsDlg (new ManagePacketsDialog (this)),
00134     delegate (0)
00135 {
00136     menu->addAction (tr ("Manage Packets"), this, SLOT (managePackets()), QKeySequence (tr ("Ctrl+M", "File|Exit")));
00137 }
00138 
00139 PacketDumpTreeView::~PacketDumpTreeView()
00140 {
00141     delete menu;
00142     delete managePacketsDlg;
00143 //  delete delegate;
00144 }
00145 
00146 void PacketDumpTreeView::mouseReleaseEvent (QMouseEvent *mouseEvent)
00147 {
00148     if (mouseEvent->button() == Qt::LeftButton) return QTreeView::mouseReleaseEvent (mouseEvent);
00149 
00150 //  LeftMouseClickDialog *dialog = new LeftMouseClickDialog(mouseEvent->pos(), this);
00151 //  QMenuBar *menuBar = new QMenuBar (this);
00152     const QPoint pos = mouseEvent->globalPos();
00153 
00154     menu->move (pos);
00155     managePacketsDlg->move (pos);
00156     menu->show();
00157 //  menuBar->addMenu (menu);
00158 //  menuBar->move (pos);
00159 //  menuBar->show();
00160 //  dialog->exec();
00161 
00162     return QTreeView::mouseReleaseEvent (mouseEvent);
00163 }
00164 
00165 void PacketDumpTreeView::managePackets()
00166 {
00167     PacketModel *packetModel = static_cast<PacketModel*> (model());
00168 
00169     managePacketsDlg->setPacketFlags (packetModel->packetFlags);
00170     managePacketsDlg->setModal (true);
00171     managePacketsDlg->exec();
00172 
00173     PacketFlags flags = managePacketsDlg->packetFlags();
00174 
00175     packetModel->setFlags (flags);
00176 }
00177 
00178