Class: ImageViewer
- Defined in:
- ext/ruby/qtruby/examples/widgets/imageviewer/imageviewer.rb
Instance Method Summary collapse
- #about ⇒ Object
- #adjustScrollBar(scrollBar, factor) ⇒ Object
- #createActions ⇒ Object
- #createMenus ⇒ Object
- #fitToWindow ⇒ Object
-
#initialize(parent = nil) ⇒ ImageViewer
constructor
A new instance of ImageViewer.
- #normalSize ⇒ Object
- #open ⇒ Object
- #print ⇒ Object
- #scaleImage(factor) ⇒ Object
- #updateActions ⇒ Object
- #zoomIn ⇒ Object
- #zoomOut ⇒ Object
Constructor Details
#initialize(parent = nil) ⇒ ImageViewer
Returns a new instance of ImageViewer.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'ext/ruby/qtruby/examples/widgets/imageviewer/imageviewer.rb', line 37 def initialize(parent = nil) super(parent) @imageLabel = Qt::Label.new @imageLabel.backgroundRole = Qt::Palette::Base @imageLabel.setSizePolicy(Qt::SizePolicy::Ignored, Qt::SizePolicy::Ignored) @imageLabel.scaledContents = true @scrollArea = Qt::ScrollArea.new @scrollArea.backgroundRole = Qt::Palette::Dark @scrollArea. = @imageLabel setCentralWidget(@scrollArea) createActions() createMenus() setWindowTitle(tr("Image Viewer")) resize(500, 400) @printer = Qt::Printer.new end |
Instance Method Details
#about ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'ext/ruby/qtruby/examples/widgets/imageviewer/imageviewer.rb', line 117 def about() Qt::MessageBox::about(self, tr("About Image Viewer"), tr("<p>The <b>Image Viewer</b> example shows how to combine Qt::Label " + "and Qt::ScrollArea to display an image. Qt::Label is typically used " + "for displaying a text, but it can also display an image. " + "Qt::ScrollArea provides a scrolling view around another widget. " + "If the child widget exceeds the size of the frame, Qt::ScrollArea " + "automatically provides scroll bars. </p><p>The example " + "demonstrates how Qt::Label's ability to scale its contents " + "(Qt::Label::scaledContents), and Qt::ScrollArea's ability to " + "automatically resize its contents " + "(Qt::ScrollArea.widgetResizable), can be used to implement " + "zooming and scaling features. </p><p>In addition the example " + "shows how to use Qt::Painter to print an image.</p>")) end |
#adjustScrollBar(scrollBar, factor) ⇒ Object
215 216 217 218 |
# File 'ext/ruby/qtruby/examples/widgets/imageviewer/imageviewer.rb', line 215 def adjustScrollBar(scrollBar, factor) scrollBar.value = factor * scrollBar.value + ((factor - 1) * scrollBar.pageStep/2) end |
#createActions ⇒ Object
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 |
# File 'ext/ruby/qtruby/examples/widgets/imageviewer/imageviewer.rb', line 133 def createActions() @openAct = Qt::Action.new(tr("&Open..."), self) @openAct.shortcut = Qt::KeySequence.new(tr("Ctrl+O")) connect(@openAct, SIGNAL('triggered()'), self, SLOT('open()')) @printAct = Qt::Action.new(tr("&Print..."), self) @printAct.shortcut = Qt::KeySequence.new(tr("Ctrl+P")) @printAct.enabled = false connect(@printAct, SIGNAL('triggered()'), self, SLOT('print()')) @exitAct = Qt::Action.new(tr("E&xit"), self) @exitAct.shortcut = Qt::KeySequence.new(tr("Ctrl+Q")) connect(@exitAct, SIGNAL('triggered()'), self, SLOT('close()')) @zoomInAct = Qt::Action.new(tr("Zoom &In (25%)"), self) @zoomInAct.shortcut = Qt::KeySequence.new(tr("Ctrl++")) @zoomInAct.enabled = false connect(@zoomInAct, SIGNAL('triggered()'), self, SLOT('zoomIn()')) @zoomOutAct = Qt::Action.new(tr("Zoom &Out (25%)"), self) @zoomOutAct.shortcut = Qt::KeySequence.new(tr("Ctrl+-")) @zoomOutAct.enabled = false connect(@zoomOutAct, SIGNAL('triggered()'), self, SLOT('zoomOut()')) @normalSizeAct = Qt::Action.new(tr("&Normal Size"), self) @normalSizeAct.shortcut = Qt::KeySequence.new(tr("Ctrl+S")) @normalSizeAct.enabled = false connect(@normalSizeAct, SIGNAL('triggered()'), self, SLOT('normalSize()')) @fitToWindowAct = Qt::Action.new(tr("&Fit to Window"), self) @fitToWindowAct.enabled = false @fitToWindowAct.checkable = true @fitToWindowAct.shortcut = Qt::KeySequence.new(tr("Ctrl+F")) connect(@fitToWindowAct, SIGNAL('triggered()'), self, SLOT('fitToWindow()')) @aboutAct = Qt::Action.new(tr("&About"), self) connect(@aboutAct, SIGNAL('triggered()'), self, SLOT('about()')) @aboutQtAct = Qt::Action.new(tr("About &Qt"), self) connect(@aboutQtAct, SIGNAL('triggered()'), $qApp, SLOT('aboutQt()')) end |
#createMenus ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'ext/ruby/qtruby/examples/widgets/imageviewer/imageviewer.rb', line 175 def createMenus() @fileMenu = Qt::Menu.new(tr("&File"), self) @fileMenu.addAction(@openAct) @fileMenu.addAction(@printAct) @fileMenu.addSeparator() @fileMenu.addAction(@exitAct) @viewMenu = Qt::Menu.new(tr("&View"), self) @viewMenu.addAction(@zoomInAct) @viewMenu.addAction(@zoomOutAct) @viewMenu.addAction(@normalSizeAct) @viewMenu.addSeparator() @viewMenu.addAction(@fitToWindowAct) @helpMenu = Qt::Menu.new(tr("&Help"), self) @helpMenu.addAction(@aboutAct) @helpMenu.addAction(@aboutQtAct) ().addMenu(@fileMenu) ().addMenu(@viewMenu) ().addMenu(@helpMenu) end |
#fitToWindow ⇒ Object
106 107 108 109 110 111 112 113 114 |
# File 'ext/ruby/qtruby/examples/widgets/imageviewer/imageviewer.rb', line 106 def fitToWindow() fitToWindow = @fitToWindowAct.checked? @scrollArea. = fitToWindow if !fitToWindow @imageLabel.adjustSize() end updateActions() end |
#normalSize ⇒ Object
101 102 103 104 |
# File 'ext/ruby/qtruby/examples/widgets/imageviewer/imageviewer.rb', line 101 def normalSize() @imageLabel.adjustSize() @scaleFactor = 1.0 end |
#open ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'ext/ruby/qtruby/examples/widgets/imageviewer/imageviewer.rb', line 57 def open() fileName = Qt::FileDialog::getOpenFileName(self, tr("Open File"), Qt::Dir.currentPath()) if !fileName.nil? image = Qt::Image.new(fileName) if image.nil? Qt::MessageBox.information(self, tr("Image Viewer"), tr("Cannot load %s." % fileName)) return end @imageLabel.pixmap = Qt::Pixmap.fromImage(image) @scaleFactor = 1.0 @printAct.enabled = true @fitToWindowAct.enabled = true updateActions() if !@fitToWindowAct.checked? @imageLabel.adjustSize end end end |
#print ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'ext/ruby/qtruby/examples/widgets/imageviewer/imageviewer.rb', line 80 def print() dialog = Qt::PrintDialog.new(@printer, self) if dialog.exec painter = Qt::Painter.new(@printer) rect = painter. size = @imageLabel.pixmap.size size.scale(rect.size(), Qt::KeepAspectRatio) painter. = Qt::Rect.new(rect.x, rect.y, size.width, size.height) painter.window = @imageLabel.pixmap.rect painter.drawPixmap(0, 0, @imageLabel.pixmap) end end |
#scaleImage(factor) ⇒ Object
204 205 206 207 208 209 210 211 212 213 |
# File 'ext/ruby/qtruby/examples/widgets/imageviewer/imageviewer.rb', line 204 def scaleImage(factor) @scaleFactor *= factor @imageLabel.resize(@imageLabel.pixmap().size() * @scaleFactor) adjustScrollBar(@scrollArea.horizontalScrollBar(), factor) adjustScrollBar(@scrollArea.verticalScrollBar(), factor) @zoomInAct.enabled = @scaleFactor < 3.0 @zoomOutAct.enabled = @scaleFactor > 0.333 end |
#updateActions ⇒ Object
198 199 200 201 202 |
# File 'ext/ruby/qtruby/examples/widgets/imageviewer/imageviewer.rb', line 198 def updateActions() @zoomInAct.enabled = !@fitToWindowAct.checked? @zoomOutAct.enabled = !@fitToWindowAct.checked? @normalSizeAct.enabled = !@fitToWindowAct.checked? end |
#zoomIn ⇒ Object
93 94 95 |
# File 'ext/ruby/qtruby/examples/widgets/imageviewer/imageviewer.rb', line 93 def zoomIn() scaleImage(1.25) end |
#zoomOut ⇒ Object
97 98 99 |
# File 'ext/ruby/qtruby/examples/widgets/imageviewer/imageviewer.rb', line 97 def zoomOut() scaleImage(0.8) end |