Skip to main content

Posts

Showing posts from 2013

[Qt] Custom QListView delegate with word wrap

Using QListView with a custom item layout has been a very popular demand by many developers. The problem with using  a custom delegate was having some basic features like word wrap depending on the allowed list size. Here is a screenshot from a project prototype I've been working on. The text will wrap around to fit in multiple lines depending on text length and list view width. My custom delegate is about using two important methods: QFontMetrics::boundingRect() and QPainter::drawText() . The first method will return the minimum Rect needed to draw the given text using the given origin and font. The second method draws the given text within the given Rect. Note that the flag Qt::TextWordWrap is given to both methods when called. The code is simple: Get system default font. Process the font to suite your needs. In my case: I have a normal font and another bold one for the header. Create a QFontMetrics instance for each font. Use the QFontMetrics instances to cal...

حصن المسلم لسعيد بن علي بن وهف القحطاني للمبرمجين بصيغة txt

هذا الملف بصيغة txt كان ضمن مشروع شخصي أعمل عليه و وجدت أن الفائدة تعمّ بنشره: كتاب "حصن المسلم من أذكار الكتاب و السنة" لسعيد بن علي بن وهف القحطاني. كل سطر من الملف يساوي دعاءً أو ذكرا من الكتاب. حيث يكون بالصيغة التالية: رقم الفصل|عنوان الفصل|رقم الدعاء|الدعاء مثال: 7|دعاء الخروج من الخلاء|11|"غُفْرانَكَ". و في حال وجود أي خطأ إملائي أو غيره أرجو إخباري لتعديله و إعادة رفعه. التحميل من هنا

Reply Machine: Static XML/JSON replies for application testing

Update : Reply Machine is now Echo : A free online service to create custom JSON/XML/HTML/Plain replies for application testing. As I'm moving forward with small applications for learning purposes, here comes a new web application: Reply Machine. Reply Machine is a free online service to create static XML, JSON, and raw replies for application testing. This service can help in a situation like when I need to create a simple reply that does not deserve to setup a local service, or when I want to integrate my application with a colleague who knows nothing about web development. The idea is simple:  Write the reply you wish to receive Set the reply type (xml, json, raw data) Hit the 'create' button. The output is a link to use in your application for testing There are some advanced options as well: Delay : In case you want to delay the time between your request and server reply. Encryption : If you have sensitive data in your reply, you can save t...

Diamond Reaction: An HTML5 Chain Reaction Game using Crafty JS

I've been away from blogging for a long time. Maybe because I've not been learning code-detailed topics. I've been more like learning the basics in topics I've never tries before (like PHP) and learning some theoretical topics. Anyways, this time I'm trying a new approach to gain more experience. This was I can gain experience from anything I want to learn. So here is my almost-full game implementation of an HTML5 chain reaction game called: Diamond Reaction. Play Diamond Reaction now Let's talk a bit about it: Idea: I wanted to make a game for fun, with the focus on game mechanics. So I started thinking of some ideas that can be fun for a 'mario-lover' like me. Whenever I think of an idea, I search about it and see how frequent I find the same mechanics on the web. Till I arrived at this concept that did not have a big hit before: chain reaction games. Theme: If you look at a game like Angry Birds, you can find the game mechanics are no...

Crafty JS Tutorial (DX Ball / Breakout)

Introduction: This Crafty JS tutorial is part of Alkottab Studio's internship program, to take developers with no previous game development experience in the quickest way to production phase. Prerequisites: Previous development experience. The very basics of HTML/CSS/Javascript. Finishing the first part of the Crafty tutorial on its website. [ How Crafty works ,  Download and Setup ] Section 1: Game description: A ball continuously moving and hitting walls and bricks. Bricks are destroyed and points are collected when hit by the ball. User controls a bat and moves it right and left to prevent the ball from hitting the ground. If the ball hits the ground, a life is lost. Section 2: File Structure: Before starting the tutorial, it is better to explain how the files are arranged: index file : links to JS files, game div, some simple CSS. js directory: Crafty file and game file. assets directory: images and spritesheets used. Section 3: Coding consi...

Facebook Scores API with Koala Gem

Here is a code snippet for a piece of code that I found a lot of people asking about on the web. Since Koala gem does not have an explicit API for Facebook Scores API , some people wonder why it isn't there. Actually, there is a generic call that can do the job just fine: After getting your Koala gem to work: check this URL for more about Creating a Facebook Rails app with Koala gem . Read the set of scores for a user and their friends Facebook Scores API states that: "You can read the set of scores for a user and their friends for your app by issuing an HTTP GET request to /APP_ID/scores with the user access_token for that app." So this can be done with the 'get_object()' method, api = Koala::Facebook::API.new(session[:access_token]) scores = api.get_object(APP_ID + "/scores") Create or update a score for a user: Facebook Scores API states that: "You can post a score for a user by issuing an HTTP POST request to /USER_ID/...

Qt: Save QPainter Output in an SVG or Image File

To save what you paint in a Qt window to a file (SVG or image) is an easy and straightforward piece of code. You create a QSvgGenerator or a QImage object. Some initialization. Paint into the object (treat it as an IODevice) And  we are done. Suppose that this is what I draw in my paint event: void MainWindow::paintEvent(QPaintEvent *) { QPainter painter; painter.begin(this); painter.setRenderHint(QPainter::Antialiasing); paint(painter); painter.end(); } void MainWindow::paint(QPainter &painter) { painter.setClipRect(QRect(0, 0, 200, 200)); painter.setPen(Qt::NoPen); painter.fillRect(QRect(0, 0, 200, 200), Qt::gray); painter.setPen(QPen(Qt::white, 4, Qt::DashLine)); painter.drawLine(QLine(0, 35, 200, 35)); painter.drawLine(QLine(0, 165, 200, 165)); } If I want to save it into an SVG file, it would be something like this void MainWindow::saveSvg() { QString path = QFileDialog::getSaveFileName(this, tr("Sa...

Resizable Google Chart

To resize a Google chart (pie chart, area chart, geo chart ...) is a matter of a few lines of code after refactoring the given example in the Google Charts Gallery. The keyword is: window resize. Just keep your data in a javascript variable and put the Google Chart code in a function to be called in each onResize event. That way the chart will be redraw to fit in the new size of the chart div, and it will look like it is resizing / scaling. Note that you may want to set a min/max width/height for the size of the chart (css on the div can do the job just fine). Performance? Just fine. Unless you have large data and many details to draw, then it may be a headache to draw from the beginning at each resize (you can limit the chart resize frequency with javascript by a delay or timer). Here is a complete example: <html> <head> <script type="text/javascript" src="https://www.google.com/jsapi"></script> <script type="tex...

Camera Drag and Zoom with Mouse in Unity 3D

The script is easy and descriptive, it is the result of some searching and modification. Press the left mouse button and drag to move, and use mouse scroll wheel to zoom in and out. The zooming code can work for both orthogonal and perspective but I've been using and testing it in orthogonal mode.  Modify the public values to fit in your game. ** Just drag the script to your camera and it will work! ** C# code [CameraDragZoom.cs] using UnityEngine; using System.Collections; public class CameraDragZoom : MonoBehaviour { public float dragSpeed = -10; public int minX = -892; public int maxX = 1111; public int minZ = -880; public int maxZ = 1145; public int bottomMargin = 80; // if you have some icons at the bottom (like an RPG game) this will help preventing the drag action at the bottom public float orthZoomStep = 10.0f; public int orthZoomMaxSize = 500; public int orthZoomMinSize = 300; private bool orthograp...

How to Install Twitter Bootstrap in Rails

To install Twitter Bootstrap in a Rails application, there are several ways and gems. Here I prefer the working and straight-forward one. 1- Inside " group :assets " add: gem 'bootstrap-sass' 2- Go to " app\assets\stylesheets " directory and create a file named " bootstrap_and_overrides.css.scss " 3- Inside the previous file, add: @import "bootstrap"; @import "bootstrap-responsive"; 4- Inside " app\assets\javascripts\application.js " file, add: //= require bootstrap 5- Then in the terminal, do " bundle install " and your Rails application with be ready for Twitter Bootstrap tags.

Unity3D: Rotate Object to Face Another

I've been working on porting an HTML5 demo to Unity3D, when I stopped for more than a day just to rotate a cannon towards a point! I have to admit that Unity3D is great, but it can turn simple tasks into complex ones for no reason. Anyways, after some time with Quaternion class and Lerp function and searching for online solutions, I decided to go back to basics. The problem with Lerp is that it forces rotation in a certain time. So the cannon will rotate 30 degrees in -say- 5 seconds, and will also rotate 60 degrees in 5 seconds, which is not logical. And to solve this I have to do some extra math. So what did I do? I just rotated with a certain amount of degrees per second (using rotate() function) , and checked if the angle between my cannon and the target is within the allowed range to shoot (with a cross product of two vectors). That's it! The code works as follows: - If cannon is in attack mode, rotate towards target point with a fixed speed. - The cannon object h...

Link Preview using Rails, AJAX, and Nokogiri Gem

I was trying to make some working code to preview link content like Facebook, Google Plus, LinkedIn .. etc. And I have to say, it is not that easy to get it to perfection. Here are some notes before starting: The happy scenario is to find ready tags inside the HEAD tag of the HTML file. Some times they are there, a lot of times they are not there. In video sharing websites like Youtube or Vimeo, or other news websites who care about these details, you will find meta data to save the day. For example: <meta property="og:url" content="http://www.youtube.com/watch?v=c6nzShZQBLQ"> <meta property="og:title" content="Kinetic Scrolling Example [Arabic] [Qt]"> <meta property="og:description" content="Visit my blog entry for more info, and the complete example: http://3adly.blogspot.com/2010/11/qt-kinetic-scrolling-ariyas-example.html Example uploaded on M..."> <meta property="og:type" conten...