1
0

fb2utils.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #include "fb2utils.h"
  2. #include <QAction>
  3. #include <QFile>
  4. #include <QTextStream>
  5. #include <QToolBar>
  6. #include <QWebView>
  7. namespace FB2 {
  8. QIcon icon(const QString &name)
  9. {
  10. QIcon icon;
  11. icon.addFile(QString(":/24x24/%1.png").arg(name), QSize(24,24));
  12. icon.addFile(QString(":/16x24/%1.png").arg(name), QSize(16,16));
  13. return QIcon::fromTheme(name, icon);
  14. }
  15. QString read(const QString &filename)
  16. {
  17. // TODO: throw an exception instead of
  18. // returning an empty string
  19. QFile file( filename );
  20. if (!file.open( QFile::ReadOnly)) return QString();
  21. QTextStream in( &file );
  22. // Input should be UTF-8
  23. in.setCodec( "UTF-8" );
  24. // This will automatically switch reading from
  25. // UTF-8 to UTF-16 if a BOM is detected
  26. in.setAutoDetectUnicode( true );
  27. return in.readAll();
  28. }
  29. void addTools(QToolBar *tool, QWebView *view)
  30. {
  31. QAction *act;
  32. act = view->pageAction(QWebPage::Undo);
  33. act->setIcon(FB2::icon("edit-undo"));
  34. act->setText(QObject::tr("&Undo"));
  35. act->setPriority(QAction::LowPriority);
  36. act->setShortcut(QKeySequence::Undo);
  37. tool->addAction(act);
  38. act = view->pageAction(QWebPage::Redo);
  39. act->setIcon(FB2::icon("edit-redo"));
  40. act->setText(QObject::tr("&Redo"));
  41. act->setPriority(QAction::LowPriority);
  42. act->setShortcut(QKeySequence::Redo);
  43. tool->addAction(act);
  44. tool->addSeparator();
  45. act = view->pageAction(QWebPage::Cut);
  46. act->setIcon(FB2::icon("edit-cut"));
  47. act->setText(QObject::tr("Cu&t"));
  48. act->setPriority(QAction::LowPriority);
  49. act->setShortcuts(QKeySequence::Cut);
  50. act->setStatusTip(QObject::tr("Cut the current selection's contents to the clipboard"));
  51. tool->addAction(act);
  52. act = view->pageAction(QWebPage::Copy);
  53. act->setIcon(FB2::icon("edit-copy"));
  54. act->setText(QObject::tr("&Copy"));
  55. act->setPriority(QAction::LowPriority);
  56. act->setShortcuts(QKeySequence::Copy);
  57. act->setStatusTip(QObject::tr("Copy the current selection's contents to the clipboard"));
  58. tool->addAction(act);
  59. act = view->pageAction(QWebPage::Paste);
  60. act->setIcon(FB2::icon("edit-paste"));
  61. act->setText(QObject::tr("&Paste"));
  62. act->setPriority(QAction::LowPriority);
  63. act->setShortcuts(QKeySequence::Paste);
  64. act->setStatusTip(QObject::tr("Paste the clipboard's contents into the current selection"));
  65. tool->addAction(act);
  66. tool->addSeparator();
  67. act = view->pageAction(QWebPage::ToggleBold);
  68. act->setIcon(FB2::icon("format-text-bold"));
  69. act->setText(QObject::tr("&Bold"));
  70. tool->addAction(act);
  71. act = view->pageAction(QWebPage::ToggleItalic);
  72. act->setIcon(FB2::icon("format-text-italic"));
  73. act->setText(QObject::tr("&Italic"));
  74. tool->addAction(act);
  75. act = view->pageAction(QWebPage::ToggleStrikethrough);
  76. act->setIcon(FB2::icon("format-text-strikethrough"));
  77. act->setText(QObject::tr("&Strikethrough"));
  78. tool->addAction(act);
  79. act = view->pageAction(QWebPage::ToggleSuperscript);
  80. act->setIcon(FB2::icon("format-text-superscript"));
  81. act->setText(QObject::tr("Su&perscript"));
  82. tool->addAction(act);
  83. act = view->pageAction(QWebPage::ToggleSubscript);
  84. act->setIcon(FB2::icon("format-text-subscript"));
  85. act->setText(QObject::tr("Su&bscript"));
  86. tool->addAction(act);
  87. }
  88. }