kpilot

dbRecordEditor.cc

Go to the documentation of this file.
00001 /* KPilot
00002 **
00003 ** Copyright (C) 2003 Reinhold Kainhofer <reinhold@kainhofer.com>
00004 **
00005 **/
00006 
00007 /*
00008 ** This program is free software; you can redistribute it and/or modify
00009 ** it under the terms of the GNU General Public License as published by
00010 ** the Free Software Foundation; either version 2 of the License, or
00011 ** (at your option) any later version.
00012 **
00013 ** This program is distributed in the hope that it will be useful,
00014 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016 ** GNU General Public License for more details.
00017 **
00018 ** You should have received a copy of the GNU General Public License
00019 ** along with this program in a file called COPYING; if not, write to
00020 ** the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00021 ** MA 02110-1301, USA.
00022 */
00023 
00024 /*
00025 ** Bug reports and questions can be sent to kde-pim@kde.org
00026 */
00027 
00028 #include "options.h"
00029 
00030 #include <qlineedit.h>
00031 #include <qcheckbox.h>
00032 #include <qtooltip.h>
00033 #include <qwhatsthis.h>
00034 #include <qbuttongroup.h>
00035 #include <qlabel.h>
00036 #include <qpushbutton.h>
00037 #include <qlayout.h>
00038 
00039 #include <kmessagebox.h>
00040 
00041 #include "pilotRecord.h"
00042 #include "dbRecordEditor.h"
00043 //#include "dbRecordEditor_base.h"
00044 
00045 #include <khexedit/byteseditinterface.h>
00046 #include <khexedit/valuecolumninterface.h>
00047 #include <khexedit/charcolumninterface.h>
00048 using namespace KHE;
00049 
00050 
00051 
00052 DBRecordEditor::DBRecordEditor(PilotRecord*r, int n, QWidget *parent)
00053     : KDialogBase(parent, "RecordEditor",false,i18n("Edit Record"),
00054                 Ok|Cancel), rec(r), nr(n)
00055 {
00056 //  fWidget=new DBRecordEditorBase(this);
00057     fWidget=new QWidget(this);
00058     setMainWidget(fWidget);
00059     fBuffer = new char[4096];
00060 
00061     initWidgets();
00062     fillWidgets();
00063 }
00064 
00065 
00066 DBRecordEditor::~DBRecordEditor()
00067 {
00068     KPILOT_DELETE( fBuffer );
00069 }
00070 
00071 
00072 void DBRecordEditor::slotOk()
00073 {
00074     FUNCTIONSETUP;
00075     if (KMessageBox::questionYesNo(this, i18n("Changing the record data and flags might corrupt the whole record, or even make the database unusable. Do not change the values unless you are absolutely sure you know what you are doing.\n\nReally assign these new flags?"), i18n("Changing Record"),i18n("Assign"),KStdGuiItem::cancel())==KMessageBox::Yes)
00076     {
00077         int att=rec->attributes();
00078 #define setFlag(ctrl, flag) if (ctrl->isChecked()) att|=flag; else att &= ~flag;
00079         setFlag(fDirty, dlpRecAttrDirty);
00080         setFlag(fDeleted, dlpRecAttrDeleted);
00081         setFlag(fBusy, dlpRecAttrBusy);
00082         setFlag(fSecret, dlpRecAttrSecret);
00083         setFlag(fArchived, dlpRecAttrArchived);
00084         rec->setAttributes(att);
00085 #undef setFlag
00086 
00087         if ( fRecordDataIf->isModified() )
00088         {
00089             DEBUGKPILOT << "record data changed, new Length of record: " <<
00090                 fRecordDataIf->dataSize() << endl;
00091             // take over data
00092             rec->setData( fRecordDataIf->data(), fRecordDataIf->dataSize() );
00093         }
00094 
00095         KDialogBase::slotOk();
00096     }
00097 }
00098 
00099 void DBRecordEditor::slotCancel()
00100 {
00101     KDialogBase::slotCancel();
00102 }
00103 
00104 void DBRecordEditor::languageChange()
00105 {
00106     fRecordIndexLabel->setText( tr2i18n( "Record index:" ) );
00107     fRecordIDLabel->setText( tr2i18n( "Record ID:" ) );
00108     fRecordIndex->setText( tr2i18n( "1" ) );
00109     fRecordID->setText( tr2i18n( "1" ) );
00110     fFlagsGroup->setTitle( tr2i18n( "Flags" ) );
00111     fDirty->setText( tr2i18n( "&Dirty" ) );
00112     fDeleted->setText( tr2i18n( "De&leted" ) );
00113     fBusy->setText( tr2i18n( "&Busy" ) );
00114     fSecret->setText( tr2i18n( "&Secret" ) );
00115     fArchived->setText( tr2i18n( "&Archived" ) );
00116 }
00117 
00118 void DBRecordEditor::initWidgets()
00119 {
00120     // FUNCTIONSETUP
00121 
00122     DBRecordEditorBaseLayout = new QGridLayout( fWidget, 1, 1, 11, 6, "DBRecordEditorBaseLayout");
00123 
00124     fRecordIndexLabel = new QLabel( fWidget, "fRecordIndexLabel" );
00125     DBRecordEditorBaseLayout->addWidget( fRecordIndexLabel, 0, 0 );
00126 
00127     fRecordIDLabel = new QLabel( fWidget, "fRecordIDLabel" );
00128     DBRecordEditorBaseLayout->addWidget( fRecordIDLabel, 0, 2 );
00129 
00130     fRecordIndex = new QLineEdit( fWidget, "fRecordIndex" );
00131     fRecordIndex->setReadOnly( TRUE );
00132 
00133     DBRecordEditorBaseLayout->addWidget( fRecordIndex, 0, 1 );
00134 
00135     fRecordID = new QLineEdit( fWidget, "fRecordID" );
00136     fRecordID->setReadOnly( TRUE );
00137 
00138     DBRecordEditorBaseLayout->addWidget( fRecordID, 0, 3 );
00139 
00140     fFlagsGroup = new QButtonGroup( fWidget, "fFlagsGroup" );
00141     fFlagsGroup->setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)5,
00142         (QSizePolicy::SizeType)4, 0, 0, fFlagsGroup->sizePolicy().hasHeightForWidth() ) );
00143     fFlagsGroup->setColumnLayout(0, Qt::Vertical );
00144     fFlagsGroup->layout()->setSpacing( 6 );
00145     fFlagsGroup->layout()->setMargin( 11 );
00146     fFlagsGroupLayout = new QGridLayout( fFlagsGroup->layout() );
00147     fFlagsGroupLayout->setAlignment( Qt::AlignTop );
00148 
00149     fDirty = new QCheckBox( fFlagsGroup, "fDirty" );
00150     fFlagsGroupLayout->addWidget( fDirty, 0, 0 );
00151 
00152     fDeleted = new QCheckBox( fFlagsGroup, "fDeleted" );
00153     fFlagsGroupLayout->addWidget( fDeleted, 1, 0 );
00154 
00155     fBusy = new QCheckBox( fFlagsGroup, "fBusy" );
00156     fFlagsGroupLayout->addWidget( fBusy, 0, 1 );
00157 
00158     fSecret = new QCheckBox( fFlagsGroup, "fSecret" );
00159     fFlagsGroupLayout->addMultiCellWidget( fSecret, 1, 1, 1, 2 );
00160 
00161     fArchived = new QCheckBox( fFlagsGroup, "fArchived" );
00162     fFlagsGroupLayout->addWidget( fArchived, 0, 2 );
00163 
00164     DBRecordEditorBaseLayout->addMultiCellWidget( fFlagsGroup, 1, 1, 0, 3 );
00165 
00166     fRecordData = KHE::createBytesEditWidget( fWidget, "fRecordData" );
00167     if( fRecordData )
00168     {
00169         // fetch the editor interface
00170         fRecordDataIf = KHE::bytesEditInterface( fRecordData );
00171         Q_ASSERT( fRecordDataIf ); // This should not fail!
00172 
00173         KHE::ValueColumnInterface *ValueColumn = valueColumnInterface( fRecordData );
00174         if( ValueColumn )
00175         {
00176             ValueColumn->setNoOfBytesPerLine( 16 );
00177             ValueColumn->setResizeStyle( KHE::ValueColumnInterface::LockGrouping );
00178 //          ValueColumn->setCoding( ValueColumnInterface::HexadecimalCoding );
00179 //          ValueColumn->setByteSpacingWidth( 2 );
00180             ValueColumn->setNoOfGroupedBytes( 4 );
00181             ValueColumn->setGroupSpacingWidth( 8 );
00182         }
00183 
00184         KHE::CharColumnInterface *CharColumn = charColumnInterface( fRecordData );
00185         if( CharColumn )
00186         {
00187             CharColumn->setShowUnprintable( false );
00188 //          CharColumn->setSubstituteChar( '*' );
00189         }
00190     }
00191     else
00192     {
00193         QLabel*tmpW = new QLabel( i18n("To view and edit the record data, please install a hex editor (e.g. kbytesedit from kdeutils)."), fWidget );
00194         tmpW->setBackgroundMode( Qt::PaletteMid );
00195         tmpW->setAlignment( Qt::AlignHCenter | Qt::AlignVCenter | Qt::WordBreak);
00196         tmpW->setFrameShape( QFrame::Panel );
00197         tmpW->setFrameShadow( QFrame::Sunken );
00198         fRecordData = tmpW;
00199         fRecordDataIf = 0;
00200     }
00201 
00202     DBRecordEditorBaseLayout->addMultiCellWidget( fRecordData, 2, 2, 0, 3 );
00203 
00204     languageChange();
00205     resize( QSize(600, 561).expandedTo(minimumSizeHint()) );
00206 }
00207 
00208 void DBRecordEditor::fillWidgets()
00209 {
00210     // FUNCTIONSETUP
00211 
00212     fRecordIndex->setText(QString::number(nr));
00213     fRecordID->setText(QString::number(rec->id()));
00214 
00215     int att=rec->attributes();
00216     fDirty->setChecked(att & dlpRecAttrDirty);
00217     fDeleted->setChecked(att & dlpRecAttrDeleted);
00218     fBusy->setChecked(att & dlpRecAttrBusy);
00219     fSecret->setChecked(att & dlpRecAttrSecret);
00220     fArchived->setChecked(att & dlpRecAttrArchived);
00221 
00222     if( fRecordDataIf )
00223     {
00224         int len = rec->size();
00225         memcpy( fBuffer, rec->data(), len );
00226         fRecordDataIf->setData( fBuffer, len, 4096 );
00227         fRecordDataIf->setMaxDataSize( 4096 );
00228         fRecordDataIf->setReadOnly( false );
00229         // We are managing the buffer ourselves:
00230         fRecordDataIf->setAutoDelete( false );
00231     }
00232 }
00233 
00234 
00235 #include "dbRecordEditor.moc"