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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
|
#include <QFileDialog>
#include <QMessageBox>
#include <QApplication>
#include <QStatusBar>
#include <QLabel>
#include <QAction>
#include <QMenuBar>
#include <QToolBar>
#include <QColorDialog>
#include <QFontDialog>
#include <QDragEnterEvent>
#include <QDropEvent>
#include "mainwindow.h"
/*****************************************************************************/
/* Public methods */
/*****************************************************************************/
MainWindow::MainWindow()
{
setAcceptDrops( true );
init();
setCurrentFile("");
}
/*****************************************************************************/
/* Protected methods */
/*****************************************************************************/
void MainWindow::closeEvent(QCloseEvent *)
{
writeSettings();
}
void MainWindow::dragEnterEvent(QDragEnterEvent *event)
{
if (event->mimeData()->hasUrls())
event->accept();
}
void MainWindow::dropEvent(QDropEvent *event)
{
if (event->mimeData()->hasUrls())
{
QList<QUrl> urls = event->mimeData()->urls();
QString filePath = urls.at(0).toLocalFile();
loadFile(filePath);
event->accept();
}
}
/*****************************************************************************/
/* Private Slots */
/*****************************************************************************/
void MainWindow::about()
{
QMessageBox::about(this, tr("About QHexEdit"),
tr("The QHexEdit example is a short Demo of the QHexEdit Widget."));
}
void MainWindow::dataChanged()
{
setWindowModified(hexEdit->isModified());
}
void MainWindow::open()
{
QString fileName = QFileDialog::getOpenFileName(this);
if (!fileName.isEmpty()) {
loadFile(fileName);
}
}
void MainWindow::optionsAccepted()
{
writeSettings();
readSettings();
}
void MainWindow::findNext()
{
searchDialog->findNext();
}
bool MainWindow::save()
{
if (isUntitled) {
return saveAs();
} else {
return saveFile(curFile);
}
}
bool MainWindow::saveAs()
{
QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"),
curFile);
if (fileName.isEmpty())
return false;
return saveFile(fileName);
}
void MainWindow::saveSelectionToReadableFile()
{
QString fileName = QFileDialog::getSaveFileName(this, tr("Save To Readable File"));
if (!fileName.isEmpty())
{
QFile file(fileName);
if (!file.open(QFile::WriteOnly | QFile::Text)) {
QMessageBox::warning(this, tr("QHexEdit"),
tr("Cannot write file %1:\n%2.")
.arg(fileName)
.arg(file.errorString()));
return;
}
QApplication::setOverrideCursor(Qt::WaitCursor);
file.write(hexEdit->selectionToReadableString().toLatin1());
QApplication::restoreOverrideCursor();
statusBar()->showMessage(tr("File saved"), 2000);
}
}
void MainWindow::saveToReadableFile()
{
QString fileName = QFileDialog::getSaveFileName(this, tr("Save To Readable File"));
if (!fileName.isEmpty())
{
QFile file(fileName);
if (!file.open(QFile::WriteOnly | QFile::Text)) {
QMessageBox::warning(this, tr("QHexEdit"),
tr("Cannot write file %1:\n%2.")
.arg(fileName)
.arg(file.errorString()));
return;
}
QApplication::setOverrideCursor(Qt::WaitCursor);
file.write(hexEdit->toReadableString().toLatin1());
QApplication::restoreOverrideCursor();
statusBar()->showMessage(tr("File saved"), 2000);
}
}
void MainWindow::setAddress(qint64 address)
{
lbAddress->setText(QString("%1").arg(address, 1, 16));
}
void MainWindow::setOverwriteMode(bool mode)
{
QSettings settings;
settings.setValue("OverwriteMode", mode);
if (mode)
lbOverwriteMode->setText(tr("Overwrite"));
else
lbOverwriteMode->setText(tr("Insert"));
}
void MainWindow::setSize(qint64 size)
{
lbSize->setText(QString("%1").arg(size));
}
void MainWindow::showOptionsDialog()
{
optionsDialog->show();
}
void MainWindow::showSearchDialog()
{
searchDialog->show();
}
/*****************************************************************************/
/* Private Methods */
/*****************************************************************************/
void MainWindow::init()
{
setAttribute(Qt::WA_DeleteOnClose);
optionsDialog = new OptionsDialog(this);
connect(optionsDialog, SIGNAL(accepted()), this, SLOT(optionsAccepted()));
isUntitled = true;
hexEdit = new QHexEdit;
setCentralWidget(hexEdit);
connect(hexEdit, SIGNAL(overwriteModeChanged(bool)), this, SLOT(setOverwriteMode(bool)));
connect(hexEdit, SIGNAL(dataChanged()), this, SLOT(dataChanged()));
searchDialog = new SearchDialog(hexEdit, this);
createActions();
createMenus();
createToolBars();
createStatusBar();
readSettings();
setUnifiedTitleAndToolBarOnMac(true);
}
void MainWindow::createActions()
{
openAct = new QAction(QIcon(":/images/open.png"), tr("&Open..."), this);
openAct->setShortcuts(QKeySequence::Open);
openAct->setStatusTip(tr("Open an existing file"));
connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
saveAct = new QAction(QIcon(":/images/save.png"), tr("&Save"), this);
saveAct->setShortcuts(QKeySequence::Save);
saveAct->setStatusTip(tr("Save the document to disk"));
connect(saveAct, SIGNAL(triggered()), this, SLOT(save()));
saveAsAct = new QAction(tr("Save &As..."), this);
saveAsAct->setShortcuts(QKeySequence::SaveAs);
saveAsAct->setStatusTip(tr("Save the document under a new name"));
connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAs()));
saveReadable = new QAction(tr("Save &Readable..."), this);
saveReadable->setStatusTip(tr("Save document in readable form"));
connect(saveReadable, SIGNAL(triggered()), this, SLOT(saveToReadableFile()));
exitAct = new QAction(tr("E&xit"), this);
exitAct->setShortcuts(QKeySequence::Quit);
exitAct->setStatusTip(tr("Exit the application"));
connect(exitAct, SIGNAL(triggered()), qApp, SLOT(closeAllWindows()));
undoAct = new QAction(QIcon(":/images/undo.png"), tr("&Undo"), this);
undoAct->setShortcuts(QKeySequence::Undo);
connect(undoAct, SIGNAL(triggered()), hexEdit, SLOT(undo()));
redoAct = new QAction(QIcon(":/images/redo.png"), tr("&Redo"), this);
redoAct->setShortcuts(QKeySequence::Redo);
connect(redoAct, SIGNAL(triggered()), hexEdit, SLOT(redo()));
saveSelectionReadable = new QAction(tr("&Save Selection Readable..."), this);
saveSelectionReadable->setStatusTip(tr("Save selection in readable form"));
connect(saveSelectionReadable, SIGNAL(triggered()), this, SLOT(saveSelectionToReadableFile()));
aboutAct = new QAction(tr("&About"), this);
aboutAct->setStatusTip(tr("Show the application's About box"));
connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
aboutQtAct = new QAction(tr("About &Qt"), this);
aboutQtAct->setStatusTip(tr("Show the Qt library's About box"));
connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
findAct = new QAction(QIcon(":/images/find.png"), tr("&Find/Replace"), this);
findAct->setShortcuts(QKeySequence::Find);
findAct->setStatusTip(tr("Show the Dialog for finding and replacing"));
connect(findAct, SIGNAL(triggered()), this, SLOT(showSearchDialog()));
findNextAct = new QAction(tr("Find &next"), this);
findNextAct->setShortcuts(QKeySequence::FindNext);
findNextAct->setStatusTip(tr("Find next occurrence of the searched pattern"));
connect(findNextAct, SIGNAL(triggered()), this, SLOT(findNext()));
optionsAct = new QAction(tr("&Options"), this);
optionsAct->setStatusTip(tr("Show the Dialog to select applications options"));
connect(optionsAct, SIGNAL(triggered()), this, SLOT(showOptionsDialog()));
}
void MainWindow::createMenus()
{
fileMenu = menuBar()->addMenu(tr("&File"));
fileMenu->addAction(openAct);
fileMenu->addAction(saveAct);
fileMenu->addAction(saveAsAct);
fileMenu->addAction(saveReadable);
fileMenu->addSeparator();
fileMenu->addAction(exitAct);
editMenu = menuBar()->addMenu(tr("&Edit"));
editMenu->addAction(undoAct);
editMenu->addAction(redoAct);
editMenu->addAction(saveSelectionReadable);
editMenu->addSeparator();
editMenu->addAction(findAct);
editMenu->addAction(findNextAct);
editMenu->addSeparator();
editMenu->addAction(optionsAct);
helpMenu = menuBar()->addMenu(tr("&Help"));
helpMenu->addAction(aboutAct);
helpMenu->addAction(aboutQtAct);
}
void MainWindow::createStatusBar()
{
// Address Label
lbAddressName = new QLabel();
lbAddressName->setText(tr("Address:"));
statusBar()->addPermanentWidget(lbAddressName);
lbAddress = new QLabel();
lbAddress->setFrameShape(QFrame::Panel);
lbAddress->setFrameShadow(QFrame::Sunken);
lbAddress->setMinimumWidth(70);
statusBar()->addPermanentWidget(lbAddress);
connect(hexEdit, SIGNAL(currentAddressChanged(qint64)), this, SLOT(setAddress(qint64)));
// Size Label
lbSizeName = new QLabel();
lbSizeName->setText(tr("Size:"));
statusBar()->addPermanentWidget(lbSizeName);
lbSize = new QLabel();
lbSize->setFrameShape(QFrame::Panel);
lbSize->setFrameShadow(QFrame::Sunken);
lbSize->setMinimumWidth(70);
statusBar()->addPermanentWidget(lbSize);
connect(hexEdit, SIGNAL(currentSizeChanged(qint64)), this, SLOT(setSize(qint64)));
// Overwrite Mode Label
lbOverwriteModeName = new QLabel();
lbOverwriteModeName->setText(tr("Mode:"));
statusBar()->addPermanentWidget(lbOverwriteModeName);
lbOverwriteMode = new QLabel();
lbOverwriteMode->setFrameShape(QFrame::Panel);
lbOverwriteMode->setFrameShadow(QFrame::Sunken);
lbOverwriteMode->setMinimumWidth(70);
statusBar()->addPermanentWidget(lbOverwriteMode);
setOverwriteMode(hexEdit->overwriteMode());
statusBar()->showMessage(tr("Ready"), 2000);
}
void MainWindow::createToolBars()
{
fileToolBar = addToolBar(tr("File"));
fileToolBar->addAction(openAct);
fileToolBar->addAction(saveAct);
editToolBar = addToolBar(tr("Edit"));
editToolBar->addAction(undoAct);
editToolBar->addAction(redoAct);
editToolBar->addAction(findAct);
}
void MainWindow::loadFile(const QString &fileName)
{
file.setFileName(fileName);
if (!hexEdit->setData(file)) {
QMessageBox::warning(this, tr("QHexEdit"),
tr("Cannot read file %1:\n%2.")
.arg(fileName)
.arg(file.errorString()));
return;
}
setCurrentFile(fileName);
statusBar()->showMessage(tr("File loaded"), 2000);
}
void MainWindow::readSettings()
{
QSettings settings;
QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
QSize size = settings.value("size", QSize(610, 460)).toSize();
move(pos);
resize(size);
hexEdit->setAddressArea(settings.value("AddressArea").toBool());
hexEdit->setAsciiArea(settings.value("AsciiArea").toBool());
hexEdit->setHighlighting(settings.value("Highlighting").toBool());
hexEdit->setOverwriteMode(settings.value("OverwriteMode").toBool());
hexEdit->setReadOnly(settings.value("ReadOnly").toBool());
hexEdit->setHighlightingColor(settings.value("HighlightingColor").value<QColor>());
hexEdit->setSelectionColor(settings.value("SelectionColor").value<QColor>());
hexEdit->setAddressAreaColor(settings.value("AddressAreaColor").value<QColor>());
hexEdit->setAddressFontColor(settings.value("AddressFontColor").value<QColor>());
hexEdit->setAsciiAreaColor(settings.value("AsciiAreaColor").value<QColor>());
hexEdit->setAsciiFontColor(settings.value("AsciiFontColor").value<QColor>());
hexEdit->setHexFontColor(settings.value("HexFontColor").value<QColor>());
hexEdit->setFont(settings.value("WidgetFont").value<QFont>());
hexEdit->setAddressWidth(settings.value("AddressAreaWidth").toInt());
hexEdit->setBytesPerLine(settings.value("BytesPerLine").toInt());
hexEdit->setHexCaps(settings.value("HexCaps", true).toBool());
}
bool MainWindow::saveFile(const QString &fileName)
{
QString tmpFileName = fileName + ".~tmp";
QApplication::setOverrideCursor(Qt::WaitCursor);
QFile file(tmpFileName);
bool ok = hexEdit->write(file);
if (QFile::exists(fileName))
ok = QFile::remove(fileName);
if (ok)
{
file.setFileName(tmpFileName);
ok = file.copy(fileName);
if (ok)
ok = QFile::remove(tmpFileName);
}
QApplication::restoreOverrideCursor();
if (!ok) {
QMessageBox::warning(this, tr("QHexEdit"),
tr("Cannot write file %1.")
.arg(fileName));
return false;
}
setCurrentFile(fileName);
statusBar()->showMessage(tr("File saved"), 2000);
return true;
}
void MainWindow::setCurrentFile(const QString &fileName)
{
curFile = QFileInfo(fileName).canonicalFilePath();
isUntitled = fileName.isEmpty();
setWindowModified(false);
if (fileName.isEmpty())
setWindowFilePath("QHexEdit");
else
setWindowFilePath(curFile + " - QHexEdit");
}
QString MainWindow::strippedName(const QString &fullFileName)
{
return QFileInfo(fullFileName).fileName();
}
void MainWindow::writeSettings()
{
QSettings settings;
settings.setValue("pos", pos());
settings.setValue("size", size());
}
|